4

I have a bunch of Views in a <merge>, and I included that <merge> into a RelativeLayout. I try to refer to the IDs of those included Views to act as anchors for my other Views, but Eclipse complains that the IDs are not resolving. I found a workaround by using @+id rather than @id when I first refer to them rather than when I actually define the objects they refer to. I've already defined the two IDs in a Style and in the included <merge> where they are declared, so it feels a bit inefficient if I keep repeating the definition of the ID.

Is this the correct way of doing it? I'm assuming it's bad cause the '+' is another initialization. My current hypothesis is that you should use @+id when you first use the ID rather than when you initialize the object that the ID is going to represent, a bit like C/C++ and how they require at least a function prototype in the lines prior to the actual code that uses the function.

Another question I have is when you use the GUI-based UI builder of Eclipse, I noticed that they always use @+id rather than @id. Is this acceptable, cause it seems inefficient to me; it's as if the application will be spending more time determining whether or not the ID has been declared in R.id.

cesar
  • 8,944
  • 12
  • 46
  • 59

1 Answers1

3

Using @+id format tells the Android asset compiler to assign an ID to your element, it isn't actually an id itself. So if I use @+id/myNewId the asset compiler will create a new id named myNewId and provide a number for it. The actual number can be accessed from your code as R.id.myNewId.

If you use an @id, the compiler will look for R.id.id. You can define your own id's in XML files, as explained here: http://developer.android.com/guide/topics/resources/more-resources.html#Id. You could create your own file in res/values/[your_filename].xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item
        type="id"
        name="id_name" />
</resources>

and then refer to @id_name, for e.g.

You can also use the Id's defined in the Android namespace: @android:id/empty

This is well explained in the Android documentation: http://developer.android.com/guide/topics/ui/declaring-layout.html#id

There's also some further discussion here: android:id what is the plus sign for

Community
  • 1
  • 1
craigmj
  • 4,827
  • 2
  • 18
  • 22
  • Wait, so I can create an xml file named `id`, initialize all the IDs I need there, then never have to use `@+id` again? – cesar Jan 14 '12 at 08:01
  • I've edited to explain how, but there's no reason to, unless you want to be sure to use the same id for two different controls (which you would never want to do on the same View). I imagine the @+id was a convention added after the early developers go *so* annoyed having to add every id in the id's file whenever they wanted to add a new widget to a view. – craigmj Jan 14 '12 at 08:04
  • Don't worry, I read the document; I just wanted to confirm, but thanks for the extra effort! My problem is that I sometimes run into errors with eclipse when I use `merge` and `include` tags. Not sure if it's `adb`'s fault or android's resource xml parser's fault. Which makes me wonder when I should use the +. – cesar Jan 14 '12 at 08:26