Difference Between "@Id/" and "@+Id/" in Android

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

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.

difference between @id and @android:id

You need to use @+id when you are defining your own Id to a view, which in this case is section_spinner. And @android:id is used when you need to set an Id of a view to android's pre-defined Id in framework. for e.g when using ListActivity, TabWidget/FrameLayout in TabHost and etc.

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

Differences between @+id , @id and @android:id

@+id/mywidget

Adds the id to R.java

@id/mywidget

References an existing id

@android:id/mywidget

References an id that exists in android.R.java, e.g. resources that are already provided by the framework

difference between @android:id/* and @+id/*

The first one is using an ID defined by the Android framework (hence, the android: prefix).

The second one is declaring a new ID for use within your app.

What is the difference between android:id=@+id/btnSet and android:id=@id/btnSet?

android:id="@+id/btnSet" means you are adding a widget or view with the name btnset in your R.java file.

android:id="@id/btnSet" means you are using or referring an already defined widget or view with the name btnset

Is there any legitimate use for @+android:id?

No, you should never use @+android:id in your app. The android namespace is reserved for the framework. This has never been safe for an app to use and will now generate a compile error.

For a little background, generating new IDs within the android namespace will put them in the unused space after the existing android namespace IDs. This will work fine until new IDs are added in the framework, at which point your internal ID will overlap with an actual framework ID and cause weird issues.

As for the other instances, those should be generating errors as well. It's possible that there is a bug in AAPT. Regardless, you should remove all instances of @+android from your resources.



Related Topics



Leave a reply



Submit