Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between "@id/" and "@+id/" in Android

What is the diffirence between the @id/ and @+id/?

In @+id/ the plus symbol + instructs to create a new resource name and add in to the R.java file but what about @id/? From the documentation of ID: when referencing an Android resource ID, you do not need the plus symbol, but must add the android package namespace, like so:

android:id="@android:id/list" 

But in the image below Eclipse doesn't suggest any kind of @android:id/.

Image showing the suggestion for the @/id and @+/id

Are @id/ and @android:id/ the same?

like image 584
Vikas Patidar Avatar asked Feb 17 '11 06:02

Vikas Patidar


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 an ID in Android?

The Android unique device ID is called the Android Advertising ID (AAID). It's an anonymized string of numbers and letters generated for the device upon initial setup.

Why do we use @+ id in Android?

TL;DR: The plus sign in “@+id/” tells Android build tools that you are declaring a new resource. And if it does not exist, add to R. java file. But basically, you don't have to think, just use “@+id/” anywhere you want!

What is id in XML Android?

id. xml is generally used to declare the id's that you use for the views in the layouts. for your given xml. Any advantage in defining it in "ids.


1 Answers

you refer to Android resources , which are already defined in Android system, with @android:id/.. while to access resources that you have defined/created in your project, you use @id/..

More Info

As per your clarifications in the chat, you said you have a problem like this :

If we use android:id="@id/layout_item_id" it doesn't work. Instead @+id/ works so what's the difference here? And that was my original question.

Well, it depends on the context, when you're using the XML attribute of android:id, then you're specifying a new id, and are instructing the parser (or call it the builder) to create a new entry in R.java, thus you have to include a + sign.

While in the other case, like android:layout_below="@id/myTextView" , you're referring to an id that has already been created, so parser links this to the already created id in R.java.

More Info Again

As you said in your chat, note that android:layout_below="@id/myTextView" won't recognize an element with id myTextViewif it is written after the element you're using it in.

like image 200
Aman Alam Avatar answered Sep 17 '22 23:09

Aman Alam