Created
September 25, 2012 13:16
-
-
Save cyrilmottier/3781761 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> |
Sorry, I just saw your comment ... GitHub is not notifying me when someone is commenting a Gist :(.
Of course it uses the tag but that's not a big issue as you can still add tags using the setTag(int, Object) method.
Regarding the findViewById(int), the main issue is an identifier is supposed to be unique. Android lets you do what ever you want with it so you could do the exact same thing with findViewById(int) but it doesn't sounds right to me :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Interesting idea. Also problem is that it uses android:tag and therefore prevent you from using the tag for another use (for example a ViewHolder)
Another thing is I don't know if it's faster (in term of execution) to use that instead of a bunch of findViewById ...