with - get id from tag android

Android. Find view by tag (2)

Create ids.xml to store your id:

<?xml version="1.0" encoding="utf-8"?> <resources> <item type="id" name="component1" /> <item type="id" name="component2" /> <item type="id" name="component3" /> </resources>

Set to dynamically created component like:

Button1.setId(R.id.layout1); buttom2.setId(R.id.layout2); button3.setId(R.id.layout3);

Another way is to set a tag to your component while creating dynamically


And use getTag() to get that component

I have a dynamically created View and want to find it by tag, is this possible? I know the function findViewById, is there something similar for tags?

LinearLayout linLayout = (LinearLayout)findViewWithTag("layout1");

but I don't think you need tag for dynamic view. You can retrieve dynamic resource by following code

for (int i=0; i < total_resource; i++) {
  //retrieve id dynamically
  int id = getResources().getIdentifier("resource"+i, "id", getPackageName());
  TextView myText = (TextView) findViewById(id); // get the element