what is the difference betwenn @id/ and @+id/ in android? [duplicate]

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".


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"


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.


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"