Can I use the same id in different layout in Android?

AndroidAndroid Layout

Android Problem Overview


I am new to Android development. Is it fine to use the same ID for images and TextViews in different Layout XML files?

When eclipse auto-list them for me, it lists all the layout variables from the project, so will it collide? Till now I have not noticed any problems using the same ID in different layouts, but I am concerned in long run.

Android Solutions


Solution 1 - Android

Short answer: Yes, you can.

Long answer: You can do this because whenever you use findViewById() to get a reference to a part of your layout, the method only looks for that view in the currently inflated layout. So even if you have another view with the same ID in another layout, Android will not look for it there.

Solution 2 - Android

It is recommended that you use different ids for different layouts. On the long run, when you will have a lot of layouts and therefor a lot of ids it will get very complicated to differentiate them.

I usually name my ids like this: layoutName_elementId.

It works for me to easily find the id I'm looking for, especially when using autocomplete (I know on what layout I'm working, but I don't really know the id; in this case, with my naming strategy, I only type the layout name and it brings on all the ids of that layout).

More information on layouts and ids can be found here.

Happy coding,

Solution 3 - Android

According to developer API guides:

> An ID need not be unique throughout the entire tree, but it should be > unique within the part of the tree you are searching (which may often > be the entire tree, so it's best to be completely unique when > possible).

So the short answer is that it's not mandatory but it's a good practice to avoid possible conflicts.

Solution 4 - Android

Not recommended, because if in future you will need to refactor the view id, Android studio will refactor it in all XML files and classes and you will get into trouble.

But there are also some cases when you do need to use same id for example if you have some abstract and you reuse multiple layouts.

In case you have multiple views with same id's in your project and you need to refactor, do it manually, don't use build in IDE function, change the id in the target view inside XML layout then fix the red error inside layout.

Update:

Currently Android studio support refactoring with "refactor in current file only" option.

Update:ViewBinding

You can get casting exception if you have nested layouts (include tag) with view id's that clashed with id's in his hierarchy.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhappyhardikView Question on Stackoverflow
Solution 1 - AndroidRaghav SoodView Answer on Stackoverflow
Solution 2 - AndroidChupamobile EvangelistView Answer on Stackoverflow
Solution 3 - AndroidAlex EpeldeView Answer on Stackoverflow
Solution 4 - AndroidPavel PoleyView Answer on Stackoverflow