1

Suppose if I am using a same id for two different views in two different layouts, I can see only one refernce is created for the id in the class "id" in R.java. Actually what I think is that it should show an error when trying to add the constant with the same name in the class "id" on build .Why its not showing an error?.And how it identifies two different views with a single id?

siraj
  • 461
  • 2
  • 6
  • 17

3 Answers3

5

If you have two different people, in 2 different groups, both named Vicky, it does not matter who that person is when calling the name (=id). If you call 'Vicky' in the first group, the Vicky in THAT group responds. In android, you assign a 'group' of views to the activity by using setContentView(<layout file>) where <layout file> is the group. If you request a view by findViewWithId() with a id which is not available (as in, not in the loaded layout file), it returns null.

example:

layout1.xml contains

<View id="@+id/name1"/>
<View id="@+id/name2"/>
<View id="@+id/name3"/>

layout2.xml contains

<View id="@+id/name1"/>
<View id="@+id/name3"/>

Calling findViewById(R.id.name1) on the first and second layout file, will return the first view. But when you call findViewById(R.id.name2) on the 2nd layout, it will return null. The id DOES exist in A layout file, but simply not in the 'loaded' layout file.

Id's are not references to View objects. They are identifiers that can be used by multiple Views in different layouts. By looking for an identifier in the layout file, you can acquire a reference to the View using it.

NickL
  • 4,258
  • 2
  • 21
  • 38
1

You can use same string names for view ids in different xml files not in the same file. And that's how compiler differentiate between different view inside different layouts

Ali Imran
  • 8,927
  • 3
  • 39
  • 50
  • k.But we are finding view by just R.id.viewId .We are not mentioning any layouts . How its working? – siraj Dec 14 '12 at 06:01
  • Because in an activity you are using some unique layout for example R.layout.main – Ali Imran Dec 14 '12 at 06:03
  • Or even in case of inflated layout you are getting view like this view.finedViewbyID(R.id.some_id) So all the time you are getting views from some specific XML – Ali Imran Dec 14 '12 at 06:15
0

If you are using the same string for two different views, it will generate only ONE id, and it will be the same. This is a feature that's quite useful in RelativeLayouts for example, because you can use the @+id multiple times and know it'll refer to the same id -- the views itself would need have unique ids, but the ids might be referenced by layout-statements.

This is the way it's designed, and not an error.

323go
  • 14,143
  • 6
  • 33
  • 41
  • Then how it identifies two different views with same id. – siraj Dec 14 '12 at 05:30
  • Added clarification for use in RelativeLayouts. Within a layout, each id needs to be unique, obviously. findViewById only operates on the current layout, so the view returned would be the correct one. – 323go Dec 14 '12 at 06:12