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.
findViewWithTag(tag)
returns the View with the default tag set by setTag(tag)
as compared using tag.equals(getTag())
.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With