101

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.

kiritsuku
  • 52,967
  • 18
  • 114
  • 136
happyhardik
  • 24,987
  • 7
  • 47
  • 60

4 Answers4

147

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.

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
  • 16
    Can you give an even longer answer? Because R.id.layout_id is a static integer, and is pre-determined at compile time so findViewById() won't know the currently inflated layout. – Chloe Sep 09 '13 at 16:06
  • 24
    For the sake of anyone else that runs across this question, I wanted to answer Chloe's request. The ID would be the same for each view, but will never appear twice in the same activty. As a metaphor, consider what would happen if I gave you a stack of papers, all with a bunch of numbers on them. Several of these sheets of paper have the number 4 written on them. You're looking at one of the sheets, and I ask you to circle the 4. This is easy. Now you look at another sheet with a 4 on it. You can find and circle that as well. It doesn't matter that the number is defined in advance. – Ted Cannelongo Jul 10 '14 at 00:31
  • 7
    I don't agree with Ted's comment, you can have at the same time two components with the same ID running on the same Activity. I just had the problem on an app, I was using two different ViewPager at the same time in the main activity. When I tried to retrieve the ViewPager currently displayed with the ID, it actually retrieved the other one running in the background. I fixed the problem by simply making the two IDs unique. – Yoann Hercouet Feb 04 '16 at 17:01
  • 10
    @YoannHercouet I believe that this is because both were on the same sheet (Activity) – Naheel Jul 07 '16 at 18:38
102

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.

starball
  • 20,030
  • 7
  • 43
  • 238
  • 3
    What's the problem of looking for let's say `ok_button` in every activity of yours ? Autocompletion is even clearer that way if you know chat you're looking for. – Dan Chaltiel Oct 16 '15 at 23:36
23

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.

Alex Epelde
  • 1,679
  • 1
  • 15
  • 16
  • 1
    I once created same ids by accident. Un-renaming them later was a lot of work because all instances were reanmed. This means once you have an id conflict, you're screwed. It'll be very difficult to separate everything back again. – t3chb0t Jun 21 '20 at 10:53
  • @t3chb0t For real! Even if you check "local only" Android Studio renames all instances. It is absolutely ridiculous. – John Glen Oct 04 '20 at 22:29
19

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.

Pavel Poley
  • 5,307
  • 4
  • 35
  • 66