What is the difference between @id and @+id?
Asked Answered
O

5

48

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.

Ousel answered 22/6, 2012 at 17:9 Comment(1)
Does this answer your question? Difference between "@id/" and "@+id/" in AndroidBonnybonnyclabber
R
68

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.

Rerun answered 22/6, 2012 at 17:13 Comment(7)
Ok thank you, does this mean that I can use the @+id whenever I want with no problems?Ousel
yes, when you want to create new unique id, just use @+id as is meant by docs.Rerun
@Sajmon will it create problem when i use android:layout_below="@+id/start" instead of android:layout_below="@id/start". Or it can create problem after long time ?Ogle
Some folks have developed a habit of always using the first form (with the plus symbol). This way they don’t have to worry about undefined ID problems or think about ordering Views. It works, but only because of the Android system being forgiving of duplicate IDs, by ignoring further instructions to create a new ID with the same name. However, this may change in the future, which will cause such layouts to become broken. Even if it doesn’t, it may be confusing to other developers. At the very least, it is rather irresponsible, which doesn’t fit well into the Android methodologyUrita
Are the ids generated all together at compile time or does order matter in some sense? I have a habit of only using + for the first instance of the new id, but when I have something like android:layout_below="@+id/start" followed by the actual view with android:id="@id/start", the previewer tends to get confused and doesn't create the display. The workaround is to either have both as @+id or to have @id before @+id/startShandishandie
What If I later need to change ordering of views? In that case I should refactor all the ID's. That's why I use always @+id whenever possible. Just interesting about performance, if that's overhead for the system to check for duplicates. I think no.Beeman
@AllanW I noticed same thing. One way you can get around this (at least in AS 3.0.1) is by clicking on the flyout used to switch from blueprint to design (or both) and clicking on Force Refresh Layout. This seemed to snap everything to within the RelativeLayout.Thurlow
C
5

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/

Convector answered 13/10, 2018 at 17:6 Comment(0)
B
3

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

Burrton answered 3/11, 2015 at 3:57 Comment(0)
B
2

Sometimes you have to use + sign. E.g. when you use <include ... /> and the included file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.FloatingActionButton xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    (...)
    app:layout_anchor="@+id/view_pager"
    app:layout_anchorGravity="top|right|end"
 />

If you don't add + in "@+id/view_pager" you will get error while building project:

Error:(9, 24) No resource found that matches the given name (at 'layout_anchor' with value '@id/view_pager').

It happend to me me in project with libraries.

Berthaberthe answered 13/2, 2017 at 15:24 Comment(0)
D
0

In order to access a widget (or component) in Java or to make others dependent on it, we need a unique value to represent it. That unique value is provided by android:id attribute which essentially adds id provided as a suffix to @+id/ to the id resource file for others to query. An id for Toolbar can be defined like this,

android:id=”@+id/toolbar

The following id can now be tracked by findViewById(…) which looks for it in the res file for id, or simply R.id directory and returns the type of View in question. The other one, @id, behaves the same as findViewById(…) — looks for the component by the id provided but is reserved for layouts only. The most general use of it is to place a component relative to the component it returns.

android:layout_below=”@id/toolbar”
Duthie answered 18/10, 2017 at 0:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.