How do I find findByTag?

Let's say I dynamically generated some LinearLayout, all with different tags.

for (int i = 0; i <= 5; i++) { final LinearLayout LinLayBtn = new LinearLayout(this); LinLayBtn.setTag( "id"+String.valueOf(i) ); ... 

And now I need to somehow access this layout by specifying the tag number with another method.

 LinearLayout LinLayBtn = (LinearLayout)findViewWithTag("1"); 

What would be the best way to do this?

Thanks!

+7
source share
1 answer

Have you tried this and found a problem?

You will need to make this line

 LinearLayout LinLayBtn = (LinearLayout)findViewWithTag("1"); 

matches the naming scheme you used when setting the tag. So you would like something similar in your example:

 LinearLayout LinLayBtn = (LinearLayout)findViewWithTag("id1"); 

If you need to do many of these searches, although this is probably the best approach for storing view references in an array when creating them, so you don't need to have all the findView calls. Or, for example, @Muhammad suggested using parent.getChild (index i);

+8
source

All Articles