Reputation: 2421
Is there any way to get the ID as in, findViewById(R.id.tableLayout)
, based on it's XML tag?
For example the Java equivalent of something along these lines:
GET $id OF view WHERE 'tag' IS n
Sorry that looks more like SQL, but I thought it would make my question clearer.
I want to be able to find a view based on it's tag and then convert it to a button using something like this.
Button b = (Button) findViewById(android.R.id.button1);
Upvotes: 0
Views: 10383
Reputation: 11403
You could just use View#findViewWithTag("someTag")
to reference the view then get the id from the view.
View view = findViewWithTag("someTag");
int id = view.getId();
Same thing for a button
Button b = (Button) findViewWithTag("someTag");
int id = b.getId();
references: http://developer.android.com/reference/android/view/View.html#findViewWithTag(java.lang.Object) http://developer.android.com/reference/android/view/View.html#getId()
Edit: If your in an activity (inside onCreate
) you could get do this
// create the Activity's view
View root = LayoutInflater.from(this).inflate(R.layout.myLayout, null);
// set the view
setContentView(root);
// find another view by the tag of the root view
Button b = (Button) root.findViewWithTag("someTag");
Upvotes: 4