Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

xml

I have just started using android, and have about 5 layout files finished. However, I just realized that I have been using @id and @+id interchangeably, but I'm not sure what the exact difference between the two are.

like image 510
JuiCe Avatar asked Jun 22 '12 17:06

JuiCe


People also ask

What's the difference between @ID and @+ id?

Exactly from docs: The at-symbol (@) at the beginning of the string indicates that the XML parser should parse and expand the rest of the ID string and identify it as an ID resource. The plus-symbol (+) means that this is a new resource name that must be created and added to our resources (in the R. java file).

What is the difference between @+ id and @ID in Android?

So what is the difference between “@+id/” and “@id/”? As you can see, they are just a character in different — the plus sign (“+”). The plus sign tells the Android build tools that you are declaring a new resource.

What is the difference between id and class selector?

Difference between id and class attribute: The only difference between them is that “id” is unique in a page and can only apply to at most one element, while “class” selector can apply to multiple elements.

Is id the same as class?

The difference between Class and ID selector The difference between an ID and a class is that an ID is only used to identify one single element in our HTML. IDs are only used when one element on the page should have a particular style applied to it. However, a class can be used to identify more than one HTML element.


2 Answers

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 the XML parser should parse and expand the rest of the ID string and identify it as an ID resource. The plus-symbol (+) means that this is a 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 that are offered by the Android framework. When referencing an Android resource ID, you do not need the plus-symbol, but must add the android package 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.

like image 161
Simon Dorociak Avatar answered Sep 21 '22 00:09

Simon Dorociak


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/

like image 43
Shivam Pokhriyal Avatar answered Sep 25 '22 00:09

Shivam Pokhriyal