View getId is null Android

If you create a button or view programmatically with OnClickListener you can set a tag key before button in parent layout is added.
And get correct button by this tag getTag() instead getId() in OnClickListener etc.

Button button = new Button(getApplicationContext());
int idOfButton = button.getId(); // return -1
String sTag = (String) button.getTag(); // return "my_button"
This entry was posted in Android Examples Code and tagged , . Bookmark the permalink.

Comments are closed.