Reputation: 4245
I have about 20 different xml files, and all of them has a raitingBar on them, and I have only one Activity for all of them, so my question is , can I use the same id for all of the raitingBar on the xml files?
Upvotes: 1
Views: 299
Reputation: 29285
Yes you can, In Android all specified XML IDs finally at build time will be some integers in R.id.*
class.
For example suppose you create this XML item:
<TextView android:id="@+id/text"
... />
The statement android:id="@+id/text"
tells Android that add a new (because of +
sign) ID called text into the R.id
class. For example it takes some iteger like 123
.
At this point, you define another view in another XML file with the same ID :
<TextView android:id="@id/text"
... />
Now in your Java codes, for example you use layout_1.xml
(the first one) and then making a call findViewById(R.id.text)
. Keep in mind that all R.id.*
is type of Integers (those are not some strange objects !)
So this statement is interpreted as findViewById(123)
. If you remember you've used only layout_1.xml
file and in this file only one view exists with the ID R.id.text
= 123
. So this call will target only one view at the layout_1.xml
not all your views in another XML files. That's it :)
Note: Try avoiding use same ID in same XML file.
Upvotes: 3