44

I am looking to find all the views in a specified activity that have the tag "balloon" for example then hide them using setVisibility to GONE.

Does anyone know how to retrieve a list of views with a given tag?

joar
  • 15,077
  • 1
  • 29
  • 54
Kevin Parker
  • 16,975
  • 20
  • 76
  • 105

4 Answers4

67

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

Community
  • 1
  • 1
Shlomi Schwartz
  • 8,693
  • 29
  • 109
  • 186
  • 2
    How on earth do you get a viewgroup? – Luminaire Apr 12 '16 at 22:32
  • @Shlomi Schwartz Why you didn't write `if-else` inside `for loop`, Like `if (child instanceof ViewGroup) views.addAll(getViewsByTag((ViewGroup) child, tag)) else { // check logic on view contain tag. }` ==> To check `ViewGroup` contains tag itself? – Khaled Lela Jan 20 '17 at 09:19
  • @KhaledLela It's because if you did it that way, the result wouldn't include viewgroups that have the tag you're looking for. It would implicitly skip over them and instead only add their leaf children that have the tag. – Sollace Nov 03 '20 at 13:30
11

You could set tags to your views in this way:

someView.setContentDescription("MyTag");

And then in order to find all views with that tag you need to call:

ArrayList<View> outputViews = new ArrayList<>();
rootView.findViewsWithText(outputViews, "MyTag", FIND_VIEWS_WITH_CONTENT_DESCRIPTION); 
CookieMonster
  • 1,723
  • 1
  • 15
  • 15
  • 2
    Perfect. Worked awesome. Small and better way than the accepted answer.. – Irfan Raza Jul 02 '17 at 21:27
  • 2
    this should be accepted answer, simple and easy; but beware of localisation may change the contentDescription – Amos Sep 09 '17 at 06:37
  • 3
    Not good! `ContentDescription` is used for accessibility. Use `android:tag` or `view.setTag()` see above [accepted answer](https://stackoverflow.com/a/16262479/4244605) – t0m Dec 28 '18 at 12:31
6

One approach would be to start with the parent ViewGroup, loop through its children(and their children and so on) and then check tags on each one of them.

Samuh
  • 36,316
  • 26
  • 109
  • 116
  • 1
    I was able to do this by removing all the overlays in the mapview. – Kevin Parker Feb 25 '11 at 15:47
  • 5
    Well, you never mentioned `MapView` and overlays in your question, did you? My answer was for your question: `Does anyone know how to retrieve a list of views with a given TAG?` – Samuh Feb 27 '11 at 06:29
4

Could this API call View#findViewWithTag help? Please note that it only returns one view...

Look for a child view with the given tag. If this view has the given tag, return this view.

nicktmro
  • 2,298
  • 2
  • 22
  • 31