2

I have defined several layouts, where few id's are multiple defined. I am confused how does it work? why doesn't it give error just like we get in java code? and most importantly, how does environment decide to which component it needs to invoke?

I understand the process of id generation is automated, which gets added to R.java For instance, if we have same IDs, say "image1" in two XMLs, say "layout1" & "layout2". but in R.java it won't have 2 IDs. that means at a time it will reference only 1 component.

In case we have to use 2 XMLs in an Activity, one as activity.setcontentview(layout1) and another as a PopupWindow.setContentView(layout2). What will happen in such case?

may be its pretty basic question but am I missing something?

Ankit
  • 6,155
  • 6
  • 44
  • 67

3 Answers3

5

You can findViewById of the current view hierarchy set to the activity. You cannot have same id for the view's in the same view tree. (must be unique).

Quoting from the docs

Any View object may have an integer ID associated with it, to uniquely identify the View within the tree. When the application is compiled, this ID is referenced as an integer, but the ID is typically assigned in the layout XML file as a string, in the id attribute. This is an XML attribute common to all View objects (defined by the View class) and you will use it very often.

http://developer.android.com/guide/topics/ui/declaring-layout.html

Example

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button myButton = (Button) findViewById(R.id.my_button);
 }

Xml

  <Button android:id="@+id/my_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/my_button_text"/>  

Here

  Button myButton = (Button) findViewById(R.id.my_button);

findViewById is the method R.id.button is an int value. Will have an entry in R.java which is auto generated. Here under the same xml file under the current view tree you cannot have views with same id.

Open your R.java do not modify its content. R.java will look something like below

  public static final class id {
      public static final int my_button=0x7f080004; // this is the int value which is unique
   }

In onCreate you refer like R.id.my_button.

You can have ids same in different xml files 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. (current view tree/hierarchy).

But it is better have ids unique to avoid confusion.

Raghunandan
  • 129,147
  • 24
  • 216
  • 249
  • fair enough, but how does they store in R.java? they are not duplicated there – Ankit Jul 24 '13 at 04:56
  • @ay89 R.java is auto generated. your have an entry for the view that you add in xml. open your `R.java` under gen folder. you will find answer yourself. ALso edit your question with more details by adding few examples or code snippets. – Raghunandan Jul 24 '13 at 05:05
  • @ay89 check the edited post. i guess it answers your question now. – Raghunandan Jul 24 '13 at 05:14
  • @ay89 that's also answered check the word current view heirarchy meaning current layout inflated. if you have a dialog the current view inflated is that of the dialog. so you cannot have same ids for the inflated view (current view inflated). – Raghunandan Jul 24 '13 at 05:18
  • @Raghunandan you can have many views with the same id in a view tree (or xml layout file) – pskink Jul 24 '13 at 05:28
  • @pskink Any View object may have an integer ID associated with it, to uniquely identify the View within the tree. – Raghunandan Jul 24 '13 at 05:30
  • but still you can have many views with the same id, which according to you is not possuble – pskink Jul 24 '13 at 05:40
  • @pskink yes you are right. i will edit that. changed every where to current view tree. that same xml layout will lead to confusion. – Raghunandan Jul 24 '13 at 05:42
  • @pskink but if you have same id for the views lint will warn you about it. duplicate id already defined. – Raghunandan Jul 24 '13 at 05:57
  • this is only when you use @+id/my_id, when you use @id/my_id syntax thera are no warnings – pskink Jul 24 '13 at 06:13
  • @pskink why would you have `@id/my_id` instead of `@+id/my_id`?. The plus-symbol (+) means that this is a new resource name that must be created and added to our resources (in the R.java file). – Raghunandan Jul 24 '13 at 06:30
  • why? because you created that id in another xml layout tile? or you crated that id in res/values/ids.xml file? does it really matter? – pskink Jul 24 '13 at 06:48
  • @pskink even in that case lint will warn you not in xml but java code if you have same id. The id is already looked up in the method. It does matter that id is unique – Raghunandan Jul 24 '13 at 06:55
  • its a warning, just to help you when you by accident tried to copy/paste and forgot to change id, the real issue is that it is perfectly ok to have two or more views with the same id – pskink Jul 24 '13 at 07:06
  • @pskink try it out have two buttons under the same layout and try click listener on the same and log some info. try it yourself you will know. i will stop commenting coz this is leading to discussion. – Raghunandan Jul 24 '13 at 07:17
  • i dont know what are you trying to prove: takw a ListView as an example, say it has 5 items, each having a Button with the same id, so you say they will not work? – pskink Jul 24 '13 at 07:28
  • @pskink i am not talking about listview. see the second highlighted part of the post. for each row in listview you are inlfating a custom layout. ie assuming you have a custom layout.your current view tree is the layout inflated in that case. i am not proving anything to anyone. I will not comment an more coz this is leading to discussion. – Raghunandan Jul 24 '13 at 07:31
  • ok, so i have two Buttons in some layout having rhe same id, each one having its own click listener and both working well, i dont understand why would they not work... – pskink Jul 24 '13 at 07:40
2

Different View instances can have the same ID. This situation may arise when:

The ID is just a tool what you can use to find Views. It's practically unique in most cases, but not guaranteed.

Community
  • 1
  • 1
sebesbal
  • 71
  • 4
1

Using same id for different components in different layouts will not cause any problem. But you cannot use same id for different components in same layout.. Each time a new view is created a unique integer id is assigned to it, which can be found in the R.java file.. But even if we use same id for two components, only one entry is made in to the R.java file. However, it won't cause an error and this works if both components are in different xml layouts. [http://developer.android.com/reference/android/view/View.html][1]

please find the ID section in this page. sorry if this is not what you were looking for

user2586981
  • 100
  • 1
  • 6