0

I was searching for difference in @id/ and @+id/ in our XML layouts. I got another question in my mind, Should we use different name for every component declared in layout for our application ?

I know that we can not declare two components with same name in a layout, But if i talk about entire application. I use same name in other layouts.

Is this the correct way of coding ?

This answer quote that when we declare @+id/ there is new entry in R.java file, If i add same name component in other layout, Will it create other entry in R.java file ???

I am confused in this, and what is the right way of declaring names in XML layout.

Any help would be appreciated.

Community
  • 1
  • 1
Anuj Sharma
  • 4,294
  • 2
  • 37
  • 53

2 Answers2

2

If you use @+id notation in multiple places, it will not create a new ID value each time it's used. The compiler will reuse the same ID for each of the same name in the same app.

You can use the @id notation when you know you are referencing an existing id. This is common when using relative layouts where you want to align some view with another view of some id in the same layout.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Thanks for clarifying. – Anuj Sharma Mar 02 '16 at 07:38
  • @Doug Stevenson : But we can add same @+id in different xml layout and use in different java file all together right? In this case will it be the same id reference used by compiler? – Sreehari Mar 02 '16 at 07:42
  • @Stallion It will be the underlying integer in the R class if you use the same name multiple times with @+id. You can test this yourself by looking at the R class after compilation and using debug logging to print the IDs you find in various layouts. – Doug Stevenson Mar 02 '16 at 07:45
0

You should use the different name for each view in your application. And for difference for @id and @+id read the content in the following link you understand better Difference between "@id/" and "@+id/" in Android

Community
  • 1
  • 1
Murali krishna
  • 823
  • 1
  • 8
  • 23