Reputation: 1392
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.
Upvotes: 0
Views: 170
Reputation: 134714
You can still use findViewById()
as long as you've given it an ID. Just make sure to call findViewById()
on the layout for which the ID is unique.
Upvotes: 3
Reputation: 306
You should change
if (v.getId() == id)
return v;
to
if (v.getId().equals(id))
return v;
Upvotes: 0