Home » Android » Android – Same Id for multiple widget

Android – Same Id for multiple widget

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have multiple Activity. Some of the activity have same buttons such as “Continue” or “Cancel” etc. each of which have different actions. Now the question is, can I use same id for the button’s in different activity? That is, Can I use "android:id="@+id="continue_button" for all the continue button’s in different activity. Or should I use "continue_button_1" "continue_button_2" ...........

How to&Answers:

‘Views may have an integer id associated with them. These ids are typically assigned in the layout XML files, and are used to find specific views within the view tree…View IDs need not be unique throughout the viewtree, but it is good practice to ensure that they are at least unique within the part of the tree you are searching. ‘ (http://developer.android.com/reference/android/view/View.html)

Answer:

AFAIK you can use the same id in different activities because when you

setContentView(R.layout.splash);

and inflate layout then android itself searches for that particular id in that particular layout so no problem here.

Note: This doesn’t means that you can put the same id for different element in same layout