Skip to content Skip to sidebar Skip to footer

Android Get View Id Based On It's Tag

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

Solution 1:

You could just use View#findViewWithTag("someTag") to reference the view then get the id from the view.

Viewview= findViewWithTag("someTag");
int id = view.getId();

Same thing for a button

Buttonb= (Button) findViewWithTag("someTag");
intid= 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 viewViewroot= LayoutInflater.from(this).inflate(R.layout.myLayout, null);
// set the view
setContentView(root);
// find another view by the tag of the root viewButtonb= (Button) root.findViewWithTag("someTag");

Post a Comment for "Android Get View Id Based On It's Tag"