18

Possible Duplicate:
What is different between @+id/android:list and @id/android:list ??

What is the difference between @id/.. and @+id/..? I am not referring to the difference between @android:id/.. and @id/..

Code Example:

<Button
android:id ="@id/add_button"
/>
<Button
android:id ="@+id/remove_button"
/>

What is the difference between the two id definitions above?

Community
  • 1
  • 1
hazem
  • 495
  • 1
  • 5
  • 20
  • 3
    The plus seems to indicate the id is added, and the absence seems to indicate the id already exists. I have only seen this in practice, but not noticed its necessity... So... I would also like to know more. – 700 Software Apr 20 '11 at 13:51
  • @George Bailey this is an answer – Selvin Apr 20 '11 at 13:54
  • 1
    @+George ;) i'd also like to know. are they basically interchangeable? I've always just used @+id. Btw, by "Id already exists" are you meaning as an resource id? that makes sense. – wired00 Apr 20 '11 at 13:54

4 Answers4

39

You must use the @+ notation on the first occurrence of the ID within an XML file. The second and subsequent times you can -- and should -- drop off the + sign. This will help catch typos.

For example, suppose that you have a RelativeLayout. You have a TextView in that RelativeLayout whose android:id is @+id/label. Later on in the layout XML file, you want to refer to that TextView from another one for positioning purposes (e.g., for android:layout_below).

If you typed in android:layout_below="@+id/labbel" (note the typo), at compile time, this will be considered OK. However, at runtime, things will not work, ranging from the widget being positioned incorrectly to an outright crash, depending on Android version.

If you typed in android:layout_below="@id/labbel" (note the typo and the missing + sign), then you would get a compile error.


UPDATE

Since I wasn't clear enough the first time, apparently, let's try again.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <TextView android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="URL:"
        android:layout_alignBaseline="@+id/entry"
        android:layout_alignParentLeft="true"/>
    <EditText
        android:id="@id/entry"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/label"
        android:layout_alignParentTop="true"/>
    <Button
        android:id="@+id/ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/entry"
        android:layout_alignRight="@id/entry"
        android:text="OK" />
    <Button
        android:id="@+id/cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/ok"
        android:layout_alignTop="@id/ok"
        android:text="Cancel" />
</RelativeLayout>

Above, you will see a RelativeLayout. You will notice that the first occurrences of each ID get the + sign. The second and subsequent occurrences of each ID do not get the + sign.

You could use the + sign on all of them, but then if you make a typo, the compiler will not catch the problem.

The + sign effectively states "allocate a new ID". Not having the + sign states "use a previously-allocated ID, or fail at compile time if there is no such ID".

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • +1 Your answer is much better. – MByD Apr 20 '11 at 14:03
  • do you mean by this when i create the button for the first time i should use the plus sign and after my first compile i should remove this plus sign ? i got your idea about when i call the button in another place like android:layout_toRightOf="@id/add_button" i have to remove the plus sign but here i am talking about the button declaration in its tags i mean if i am going to create a button in my xml file what is the difference bettwen sorry for bothering you :) – hazem Apr 20 '11 at 14:13
  • 1
    He means that when you create the button and assign it an ID, use the `+` sign, but when you refer to this button (in position for example, such as `android:layout_below`) you shouldn't use the `+` sign. – MByD Apr 20 '11 at 14:18
  • @hazem: See the update to my answer. – CommonsWare Apr 20 '11 at 14:23
  • @MByD i got it the problem i created two buttons one with + sign and the other without it and both of them works fine that is really wired and drives me crazy – hazem Apr 20 '11 at 14:24
  • @CommonsWare thank you very much i got it now , all due respect for you @MByD thank you too for your help – hazem Apr 20 '11 at 14:35
  • another very informative and articulate post thank you :) – wired00 Apr 20 '11 at 14:35
  • It doesn't seem that there's a Lint check for this (ref: http://tools.android.com/tips/lint-checks -- DuplicateIds seems to be for duplicate controls with the same ID, not the same ID name using the `+id` syntax twice.) – CJBS Jul 24 '15 at 21:14
6

In the Android layout resource XML source files :

"@+id/anyId" : to add new id

"@id/anyId" : to refer existing id

You should use "@id/anyId" only when "anyId" is already added to R.java class using "@+id/anyId"

Vaibhav Jani
  • 12,428
  • 10
  • 61
  • 73
3

From Android Guide

For the ID value, you should usually use this syntax form: "@+id/name". The plus symbol, +, indicates that this is a new resource ID and the aapt tool will create a new resource integer in the R.java class, if it doesn't already exist.

So + is for assigning a new id, it will also work when using existed id but it is not necessary there.

MByD
  • 135,866
  • 28
  • 264
  • 277
3

The second one:

<Button android:id ="@+id/remove_button" />

defines a new id. You would use the first one, when you want to reference the layout element. For example, in a relative layout:

android:layout_below="@id/remove_button"
kgiannakakis
  • 103,016
  • 27
  • 158
  • 194