0

I am defining an activity layout in two xml files (default and landscape orientation). In both cases, I am using the same views changing only their position and formatting.
which is the correct way to define id attribute of a view:

android:id="@+id/example_text_view"  //using @+id in both xml files

Or:

android:id="@+id/example_text_view"  //default orientation xml

android:id="@id/example_text_view"   //landscape orientation xml

Both seem to work properly unless I entierly remove id declaration from land xml file.

tomwyr
  • 1,351
  • 2
  • 16
  • 21
  • Did you see this: http://stackoverflow.com/questions/5025910/difference-between-id-and-id-in-android/5025971#5025971 ? – Gennadii Saprykin Mar 04 '16 at 23:49
  • I know the difference between them, the thing I wonder is whether view in vertical layout and corresponding one in landscape layout are distinct objects and they both need individual IDs, or these two are referencing the same object in memory? – tomwyr Mar 05 '16 at 09:03

2 Answers2

1

The difference is that the + will create a new id in R.java where as the other wont. Therefore, when you are referencing an id, don't include +. Look at this link for more info on this stuff

Zaid Qureshi
  • 1,203
  • 1
  • 10
  • 15
  • As far as I understand, if orientation is changed, another layout file will be inflated into new objects. That way I need to have different IDs specified for both views, is that right? – tomwyr Mar 05 '16 at 09:13
  • It is referenced by ID, so you if you are changing your layout based on orientation, you should give both layouts different IDs. – Zaid Qureshi Mar 05 '16 at 20:30
0

You should use

android:id="@+id/example_text_view" 

Basically, always that you are using the attribute android:id you should use @+id because you are defining an id for the view.

You should use @id when you are referencing another view as Zaid said. For example:

android:layout_below="@id/example_text_view"

This way you are saying that this view should be below the one that has the id "example_text_view"