Thursday, 19 September 2013

How can I get a view by id from a layout in Android?

How can I get a view by id from a layout in Android?

This is the only way I can find but it seems hacky:
public View getViewByIdFromLayout(int id) {
for (int i = 0; i < layout.getChildCount(); i++) {
View v = layout.getChildAt(i);
if (v.getId() == id)
return v;
}
return null;
}
Is there a better way to do this? These views were created
programmatically, not via xml, and the views' ids will be unique in a
given layout but may be reused in other layouts.

No comments:

Post a Comment