ChatGPT解决这个技术问题 Extra ChatGPT

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

https://i.stack.imgur.com/zgbUz.png

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

From android docs: (took forever to find this, and I was looking up an unrelated issue) developer.android.com/guide/topics/resources/…

b
blahdiblah

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.


@MarcoW That helped me immensely. I was having trouble finding the solution to this problem (or rather formulating the right search entry). I guess it might deserve a question+answer of its own.
Thank you! I had the same problem, and the documentation doesn't say much about this topic. But it's obvious: The problem we had here is not a frequent one ...
I always use @+id/ at android:id and android:layout_below, is that a problem?
What pisses me off is that using the "Graphical Layout" in Eclipse to drag widgets to a layout, the generated XML will contain references to other widgets using the plus, for example android:layout_below="@+id/numberPicker1" and android:layout_alignBaseline="@+id/button1". :-( I'll shut up now before I start cussing.
@melanke: Putting the + into layout_below as well as android:id is really fine, since The '+' means to create the symbol if it doesn't already exist as Tanmay Mandal mentioned in his nice (yet underrated) answer. Because of that, there is also no negative runtime effect (possibly an unmeasurably small increase in compile time though ^^).
V
Vikas Patidar

the + sign is a short cut to add the id to your list of resource ids. Otherwise you need to have them in a xml file like this

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="my_logo" type="id"/>
</resources>

@schwiz: And how can we use it in our layout?
imagine you have the resource file I defined in my answer, then in your layout you can have <View android:id="@id/my_logo"/> If you did not have the resource file I defined above then you would need to do it like <View android:id="@+id/my_logo"/> note: you only need to do the +id one time so if in another layout file you did <View android:id="+id/my_logo"/> you would not need to include the '+' character the next time you use the same id in a layout.
@schwiz: Okay that's useful but where do we have to put that file res/drawable/ or res/layout/ or res/values/ and what will be the name of that file? If you have any useful link regarding this then please post here.
Here is everything you need to know about naming conventions, etc for your resources. You would put the file in res/values/ and the convention is to name it ids but you can call it whatever you want. developer.android.com/guide/topics/resources/…
A
AVI

In Short

android:id="@+id/my_button"

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

while

android:layout_below="@id/my_button"

it just help to refer the already generated id..


P
Pang

The plus sign (+) before the resource type is needed only when you're defining a resource ID for the first time. When you compile the app, the SDK tools use the ID name to create a new resource ID in your project's R.java file that refers to the EditText element. With the resource ID declared once this way, other references to the ID do not need the plus sign. Using the plus sign is necessary only when specifying a new resource ID and not needed for concrete resources such as strings or layouts. See the sidebox for more information about resource objects.

From: https://developer.android.com/training/basics/firstapp/building-ui.html


F
Fortran

Its very simple:

"@+..." - create new

"@..." - link on existing

Source: https://developer.android.com/guide/topics/resources/layout-resource.html#idvalue


J
Jeff Axelrod

From the Developer Guide:

android:id="@+id/my_button"

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, like so:

android:id="@android:id/empty"


j
josephus

There's a bug with Eclipse where sometimes if you just created a new @+id/.., it won't be added immediately to the R.java file, even after clean-building the project. The solution is to restart Eclipse.

This I think should be solved as soon as possible, because it may (and from experience, will) confuse some developers into thinking that there's something wrong with their syntax, and try to debug it even if there's really nothing to debug.


One of the reasons why I switched to Android Studio and never looked back :) (I know it was not there yet at the time you wrote your answer)
T
Tanuj Nayak

Android uses some files called resources where values are stored for the XML files.

Now when you use @id/ for an XML object, It is trying to refer to an id which is already registered in the values files. On the other hand, when you use @+id/ it registers a new id in the values files as implied by the '+' symbol.

Hope this helps :).


A
AskNilesh

@id/ and @android:id/ is not the same.

@id/ referencing ID in your application, @android:id/ referencing an item in Android platform.

Eclipse is wrong.


Yor are saying that @id/ referencing ID in your application But eclise gives Error: No resource found that matches the given name (at 'id' with value '@id/my_resource_id') when we use it to refer the the application resource. So what's the difference between the @id/ and @+id/? How could you say eclipse is wrong?
Eclipse is wrong in case of not showing you a @android:id suggestion. And you are wrong, if you are using @id/myId without declaring it with @+id/myId somewhere. All @id/xxx must be declared somewhere in your application using @+id/xxx.
Thanks for your clarification. I think here eclipse isn't wrong instead it shows suggestion based on the context of our layout elements. As it shows @id/ only in case of RelativeLayout
R
Ray Hunter

Difference between @+id and @id is:

@+id is used to create an id for a view in R.java file.

@id is used to refer the id created for the view in R.java file.

We use @+id with android:id="", but what if the id is not created and we are referring it before getting created(Forward Referencing).

In that case, we have use @+id to create id and while defining the view we have to refer it.

Please refer the below code:

<RelativeLayout>

     <TextView
        android:id="@+id/dates"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/spinner" />

   <Spinner
     android:id="@id/spinner"
     android:layout_width="96dp"
     android:layout_height="wrap_content"
     android:layout_below="@id/dates"
     android:layout_alignParentRight="true" />

</RelativeLayout>

In the above code,id for Spinner @+id/spinner is created in other view and while defining the spinner we are referring the id created above.

So, we have to create the id if we are using the view before the view has been created.


S
Suraj Vaishnav

If the view item performs the same operation, you can use the @+id for each entry in any layout because during the compilation of multiple @+id/foo the R.java file only creates one enumeration. So for example, if I have a save button on each page that performs the same operation, I use android:id="@+id/button_save" in each layout. The R.java file only has one entry for the button_save.


A
AskNilesh

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

The first one is used for to create the ID of the particular ui component and the another one is used for to refer the particular component