Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android findViewWithTag for views with multiple tags

Tags:

android

I used to set a tag to a view using view.setTag(t1); and then get the view using parent.findViewWithTag(t1); which returned correctly.

I now need to set 2 different tags for my views and I am doing that using

view.setTag(R.id.tag1, t1);
view.setTag(R.id.tag2, t2);

Where tag1 and tag2 are ids declared in res/values/ids.xml

I am then trying to get the view with tag t1, but parent.findViewWithTag(t1); returns null. I searched and there is no method findViewWithTag or similar that would also accept the key of the tag.

Is there any way to achieve this? If there isn't could you point me to where it's stated in the android documentation?

In this specific case, I could use id instead of one of the tags, but for situations where that is not possible, I would like to know if it can be achieved using tags.

like image 784
Scorpio Avatar asked Apr 22 '16 14:04

Scorpio


2 Answers

findViewWithTag(tag) returns the View with the default tag set by setTag(tag) as compared using tag.equals(getTag()).

like image 50
Diego Torres Milano Avatar answered Sep 27 '22 17:09

Diego Torres Milano


as a complement to Diegos answer I would like to point it out on the source code:

https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/view/View.java#18288

if (tag != null && tag.equals(mTag)) {

the tag used to compare and find the view is the mTag which is set by using the direct method setTag(Object) as seen on the source code on this line:

https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/view/View.java#18505

public void setTag(final Object tag) {
    mTag = tag;
}
like image 31
Budius Avatar answered Sep 27 '22 19:09

Budius