-
-
Save markus2610/3782038 to your computer and use it in GitHub Desktop.
An implementation of a "findViewsByTag" method on Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<resources> | |
<string name="tag">tag:touchMe</string> | |
<string name="touch_me">Touch me!</string> | |
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.cyrilmottier.android.tests; | |
import android.view.View; | |
import android.view.ViewGroup; | |
/** | |
* @author Cyril Mottier | |
*/ | |
public class ViewAdditions { | |
public interface OnTagFoundHandler { | |
void onTagFound(View v); | |
} | |
public static final void findViewsByTag(View root, String tag, OnTagFoundHandler handler) { | |
if (root == null) { | |
throw new NullPointerException(); | |
} | |
if (tag == null || handler == null) { | |
return; | |
} | |
if (tag.equals(root.getTag())) { | |
handler.onTagFound(root); | |
} | |
if (root instanceof ViewGroup) { | |
final ViewGroup rootGroup = (ViewGroup) root; | |
final int childCount = rootGroup.getChildCount(); | |
for (int i = 0; i < childCount; i++) { | |
findViewsByTag(rootGroup.getChildAt(i), tag, handler); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.cyrilmottier.android.tests; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import android.view.MotionEvent; | |
import android.view.View; | |
import android.view.View.OnTouchListener; | |
import android.widget.Toast; | |
import com.cyrilmottier.android.tests.ViewAdditions.OnTagFoundHandler; | |
/** | |
* @author Cyril Mottier | |
*/ | |
public class ViewAdditionsActivity extends Activity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.view_additions_activity); | |
ViewAdditions.findViewsByTag(findViewById(R.id.root), getString(R.string.tag), new OnTagFoundHandler() { | |
@Override | |
public void onTagFound(View v) { | |
v.setOnTouchListener(mOnTouchListener); | |
} | |
}); | |
} | |
private OnTouchListener mOnTouchListener = new OnTouchListener() { | |
@Override | |
public boolean onTouch(View v, MotionEvent event) { | |
switch (event.getAction()) { | |
case MotionEvent.ACTION_DOWN: | |
Toast.makeText(getApplicationContext(), "ACTION_DOWN fired", Toast.LENGTH_SHORT).show(); | |
break; | |
} | |
return true; | |
} | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/root" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center" | |
android:columnCount="2" | |
android:useDefaultMargins="true" > | |
<TextView | |
android:background="#dedede" | |
android:ellipsize="end" | |
android:gravity="center" | |
android:padding="16dp" | |
android:tag="@string/tag" | |
android:text="@string/touch_me" /> | |
<TextView | |
android:background="#dedede" | |
android:ellipsize="end" | |
android:gravity="center" | |
android:padding="16dp" | |
android:tag="@string/tag" | |
android:text="@string/touch_me" /> | |
<TextView | |
android:background="#dedede" | |
android:ellipsize="end" | |
android:gravity="center" | |
android:padding="16dp" | |
android:tag="@string/tag" | |
android:text="@string/touch_me" /> | |
<TextView | |
android:background="#dedede" | |
android:ellipsize="end" | |
android:gravity="center" | |
android:padding="16dp" | |
android:tag="@string/tag" | |
android:text="@string/touch_me" /> | |
</GridLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment