What is the difference between @id and @+id? What is the difference between @id and @+id? xml xml

What is the difference between @id and @+id?


You need to use @+id when you are defining your own Id for a View.

Exactly from docs:

The at-symbol (@) at the beginning of the string indicates that theXML parser should parse and expand the rest of the ID string andidentify it as an ID resource. The plus-symbol (+) means that this isa new resource name that must be created and added to our resources(in the R.java file). There are a number of other ID resources thatare offered by the Android framework. When referencing an Androidresource ID, you do not need the plus-symbol, but must add the androidpackage namespace.

Here is a practical example:

<Button    android:id="@+id/start"   android:layout_width="wrap_content"   android:layout_height="wrap_content"/><Button    android:id="@+id/check"   android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:layout_below="@id/start"/>

So here, you created two IDs, start and check. Then, in your application you are able to connect to them with findViewById(R.id.start).

And this android:layout_below="@id/start" refer to existing id.start and means that your Button with id check will be positioned below Button with id start.


All the other answers forgot to mention this one little thing.

When using @id/ to refer to an already generated android resource, make sure that the resource you are referring to is defined earlier and not later.

That is Instead of this:

<Button  android:id="@+id/check" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/start"  /><Button  android:id="@+id/start" android:layout_width="wrap_content" android:layout_height="wrap_content" />

Use this:

<Button  android:id="@+id/start" android:layout_width="wrap_content" android:layout_height="wrap_content" /><Button  android:id="@+id/check" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/start" />

In the first example you are referring to a resource @id/start which is generated after you are accessing it. Although this would work in case of native android, but if you are going to use this code in react-native or ionic or any other hybrid platform, it would generate resource not found error.

So be careful to generate the resource id before using it as @id/


android:id="@+id/my_button"

+id Plus sing tells android to add or create a new id in Resources.

android:layout_below="@id/my_button"

it just help to refer the already generated id..