Andriy Drozdyuk
Andriy Drozdyuk

Reputation: 61061

Is android:id specific to xml file or the whole project?

Is the android:id="@+id/somevalue" specific to the xml file in which it is defined, or the whole android project?

If it is project-wide, it seems like I have a lot of id's to come up for text fields. For example I have to name my "title" field like "title1" and "title2" etc...

Upvotes: 3

Views: 126

Answers (1)

Cristian
Cristian

Reputation: 200090

They are unique on the whole project but can be used on different contexts. The reason is fairly simple: there will be just one R.id.name_of_id variable, though, they cannot be reference from anywhere. I mean, if you have and ID called @+id/my_id which is on the my_layout.xml file, you cannot use it unless you are currently working with my_layout.xml implicitly or explicitly. Implicity by using findViewById after a setContentView(R.layout.my_layout.xml) call (from the activity), or explicity by using it from an object (for instance, when you 'inflate' a layout and assign it to a View object you can do object.findViewById()).

Upvotes: 4

Related Questions