Find all views with tag?
Here you go:
private static ArrayList<View> getViewsByTag(ViewGroup root, String tag){
ArrayList<View> views = new ArrayList<View>();
final int childCount = root.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = root.getChildAt(i);
if (child instanceof ViewGroup) {
views.addAll(getViewsByTag((ViewGroup) child, tag));
}
final Object tagObj = child.getTag();
if (tagObj != null && tagObj.equals(tag)) {
views.add(child);
}
}
return views;
}
I've already answered it here :Android - how to find multiple views with common attribute
How to find list of View(s) that has a specific Tag (attribute)
You shouldnt expect an array of views from this method, since the method signature itself tells that it will return a single view.
public final View findViewWithTag (Object tag)
However, what you may do is to get your layout as ViewGroup
and then iterate through all the child views to find out your desired view by doing a look-up on their tag. For example:
/**
* Get all the views which matches the given Tag recursively
* @param root parent view. for e.g. Layouts
* @param tag tag to look for
* @return List of views
*/
public static List<View> findViewWithTagRecursively(ViewGroup root, Object tag){
List<View> allViews = new ArrayList<View>();
final int childCount = root.getChildCount();
for(int i=0; i<childCount; i++){
final View childView = root.getChildAt(i);
if(childView instanceof ViewGroup){
allViews.addAll(findViewWithTagRecursively((ViewGroup)childView, tag));
}
else{
final Object tagView = childView.getTag();
if(tagView != null && tagView.equals(tag))
allViews.add(childView);
}
}
return allViews;
}
Android - how to find multiple views with common attribute
I've finally wrote this method (Updated thanks to @SuitUp (corrected username)):
private static ArrayList<View> getViewsByTag(ViewGroup root, String tag){
ArrayList<View> views = new ArrayList<View>();
final int childCount = root.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = root.getChildAt(i);
if (child instanceof ViewGroup) {
views.addAll(getViewsByTag((ViewGroup) child, tag));
}
final Object tagObj = child.getTag();
if (tagObj != null && tagObj.equals(tag)) {
views.add(child);
}
}
return views;
}
It will return all views that have android:tag="TAG_NAME"
attribute. Enjoy ;)
Android findViewWithTag for views with multiple tags
findViewWithTag(tag)
returns the View with the default tag set by setTag(tag)
as compared using tag.equals(getTag())
.
Android: Getting list of views by tag and changing specific button
instanceof
I am not an Android programmer, but it looks like your Question is a basic Java question. Are you saying:
I have an object being returned as the superclass
Object
. But I know in fact that the object is an instance of theButton
class. How can I refer to it as aButton
specifically rather than asObject
generally?
If so, simply cast the object. Going from the more general super-type to the more specific type is downcasting.
Object o = gridcells.get(0) ;
Button b = ( Button ) o ;
Or combine:
Button b = ( Button ) gridcells.get(0) ;
If not quite sure, do a check with the special operator instanceof
.
if( gridcells.get(0) instanceof Button ) {
Button b = ( Button ) gridcells.get(0) ;
}
If you are doing much casting in your code, you likely have a design problem. Usually that stems from not understanding the basics of Object-Oriented Programming. But occasionally some casting is in order and cannot be avoided.
See this similar Question for more discussion.
If I am misunderstanding the Question, you may need to re-write your Question.
Related Topics
Android Draw Route on a Mapview with Twoo Poi-S
Android Raw Folders - Creation and Reference
Speed Up 'Navigation Drawer' Animation Speed on Closing
Android: How to Get Current Opened Application Name on Screen
How to Add Code Obfuscation for My Android Application
Implementing a Simple Dagger2 Sample
How to Launch an Activity Without a Ui
Create a Chatview Layout in Android
How to Pass Parameters to Onclicklistener
Android - Way to Appear Bordered Text on the Textview
Why Can't One Add/Remove Items from an Arrayadapter
Android Change Text Color of Items in Spinner
Cannot Resolve Mapactivity Class on Android