0

When working through the tutorial to build my first Android application I reached a section where it states the @+id/ prefix not only references a resource that is defined in the gen/R.java file, but that the + sign also indicates its first encounter with it so it will create it. Consider this code snippet:

<EditText android:id="@+id/edit_message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="@string/edit_message" />

After reading through the side-bar in the first link, related to resources, and the article it linked to named Providing Resources (at a somewhat cursory level), I'm unable to get a very clear statement from the documentation on the scope of the resource with the @+id/ prefix. I understand that you can have a resource with the same name scoped inside each prefix:

Note: This string resource has the same name as the element ID: edit_message. However, references to resources are always scoped by the resource type (such as id or string), so using the same name does not cause collisions.

but what I'm driving at is this. Based on the documentation it appears that I can't have two controls resourced as edit_message in two different activities because there would be a conflict.

My concern here is that I'd presumably have to prefix my id attributes with the Activity name to keep them unique so I can access these controls from code.

Am I correct in my statement and assumptions here?

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
  • 1
    http://stackoverflow.com/questions/17825356/how-does-multiple-component-with-same-id-work-in-android/17825464#17825464. check this might help – Raghunandan Jul 31 '13 at 17:51

1 Answers1

3

You can place that exact block of XML in another layout file and it will work just fine. @+id generates a new id if it is not defined yet. When your app gets packaged, the packaging tool will create the id once and all the others will correctly refer to that id.

As long as you don't end up with two UI components in the same layout with the same id, everything's ok.

Karakuri
  • 38,365
  • 12
  • 84
  • 104
  • So even in code it will marshal the correct reference? I guess that makes sense because two activities can't be running at the same time, right? – Mike Perrenoud Jul 31 '13 at 17:54