1

Possible Duplicate:
Difference between “@id/” and “@+id/” in Android

When you create a layout XML file for Android applications, you usually declare the ID of each layout element as:

@+id/elementID

Don't you? I guess the "+" means that this element's ID is just created and therefore you need the plus, right?

But what do you have to do when you refer to a layout element before it is created? Do you refer to it with "+" and then create it without "+"? Simply put, is the following code correct (in a RelativeLayout container)?

<ImageButton
    android:id="@+id/helpButton"
    android:layout_toLeftOf="@+id/moreButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
<ImageButton
    android:id="@id/moreButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true" />
Community
  • 1
  • 1
caw
  • 30,999
  • 61
  • 181
  • 291

1 Answers1

2

I guess the "+" means that this element's ID is just created and therefore you need the plus, right?

Yes.

Do you refer to it with "+" and then create it without "+"?

Yes. The first occurrence of the ID gets the +. Second and subsequent occurrences can leave it off.

Simply put, is the following code correct (in a RelativeLayout container)?

Well, your ImageButtons are missing images... :-)

That being said, your use of the + sign there seems fine.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thank you, this is exactly what I thought. Of course, my image buttons have images, but I left them off. Can you refer to any sources that prove this? (Because others think different about this, see the other answer.) – caw Jun 02 '12 at 17:49
  • @MarcoW.: Well, http://developer.android.com/guide/topics/ui/declaring-layout.html#id, for starters. Also, see the examples of `RelativeLayout` on the Android developer site. This really only comes into play with `RelativeLayout` -- other containers do not use references to widget resources. – CommonsWare Jun 03 '12 at 00:56
  • Can't really find any proof in the Android docs (see http://developer.android.com/resources/tutorials/views/hello-relativelayout.html) but thanks, anyway! – caw Jun 03 '12 at 11:34