12

Must the resource ID's for views in XML layouts be unique across all layouts?

For example, I'm working on a little recipe manager app. I have a layout file for adding a new ingredient. In this layout I have an EditText for the ingredient that I'd like to call "edt_name". But I'm afraid that this name is too general; e.g. I might also have an EditText for a recipe name, a cooking procedure name, etc in other XML layout files.

However, I also don't want to make the labels more complex than necessary. I'd like to avoid calling the aforementioned EditText "edt_name_new_ingredient" if I could.

I'm curious as to how developers organize their resources in general. Android doesn't support sub-directories for resources as far as I know, so naming schemes can get really messy.

Tianxiang Xiong
  • 3,887
  • 9
  • 44
  • 63

2 Answers2

7

No, resource ID should not be unique across different xml layouts however they must be unique in a particular xml file.

Bohdan Kaminskyi
  • 134
  • 1
  • 11
success_anil
  • 3,659
  • 3
  • 27
  • 31
  • But when I access R.id.(view_id) in my java file, I get a list of id's from all views? I just tried naming two EditBoxes in two different XML layout files with the same name. I didn't get an error message, but when I used R.id.(view_id) I only got one view_id, and I don't know which EditText the id refers to! – Tianxiang Xiong Aug 04 '11 at 06:19
  • 5
    you are right... but Android chooses the view id from the xml layout that you have set using setContentView method in your activity, from all of the view id. that you 're getting in the list... that's why you don't get any error. Try changing the Id for edittext in two xml... then you could get a better picture of what's going on – success_anil Aug 04 '11 at 07:06
  • I just tested what you said and you're right, success_anil. R.id.(view_id) brings up all the views across all layouts, but if the view is not found in the layout you're currently using, the program will crash. So if you have two views with the same id across different layout files, it will automatically use the one in your current layout file. – Tianxiang Xiong Aug 05 '11 at 04:44
  • @xiongtx It could cause problem to use the same Id in different XML layout file when using RelativeLayout, because the same Id could occurs twice in the same tree. See this answer http://stackoverflow.com/a/6495504/62921 – ForceMagic Jul 17 '13 at 20:37
  • I think Andoid just converts the names (strings) to ids (integer) for performance. So same name will compile to same number. And that's it. Later both resources will have the same number id, but if they exist in different environments Android doesn't care. findViewById search inside the current view tree. – helios Jun 25 '18 at 17:23
2

Resource IDs are namespaced within the package. When you access a resource (in XML, for example), the package name is implicitly set to the current one. You can have other resource files in a different package and refer to those within your code or XML (this is how one accesses the platform resources that come with the SDK).

Similarly in code, you can access a different package's R class and use its resources, but all those within the same package must have unique names.

More info can be found in the documentation here.

Eugene S
  • 3,092
  • 18
  • 34
  • 1
    Thanks for the link. I'm building my application as a single project, so I only have one "R" class. I guess that means that all my views across all my layout files have to have unique id's? – Tianxiang Xiong Aug 04 '11 at 06:25