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

I just started using android, and we have about 5 layouts left. However, I just realized that I use @id and @ + id interchangeably, but I'm not sure what the exact difference between the two is.

+18
android xml
Jun 22 2018-12-12T00:
source share
4 answers

You need to use @+id when you define your own identifier for the view.

Exactly from the docs :

The at (@) character at the beginning of the line indicates that the XML parser should parse and extend the rest of the identifier string and identify it as an identifier resource. A plus sign (+) means that this is a new resource name that needs to be created and added to our resources (in the R.java file). There are a number of other identification resources that are offered by the Android platform. When you reference the Android Resource Identifier, you do not need a plus sign, but it should add the android package namespace.

And now I will add a practical example for you:

 <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, you created two IDs , start and check. Then in the application you can connect to them using findViewById(R.id.start) .

And this android:layout_below="@id/start" refers to the existing id.start and means that your Button with id check will be located below the Button with id start .

+35
Jun 22 '12 at 17:13
source share
— -

Sometimes you have to use the + sign. For example. 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 did not add + to "@+id/view_pager" , you will receive an error when creating the project:

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

I did it in a project with libraries.

+1
Feb 13 '17 at 15:24
source share

Android: ID = "@ + identifier / my _button"

+ id Plus sing says android to add or create a new identifier in the Resources section.

Android: layout_below = "@ id / my_button"

it just helps to reference the already generated identifier.

0
Nov 03 '15 at 3:57
source share

To access a widget (or component) in Java or make it dependent on others, we need a unique value to represent it. This unique value is provided by the android: id attribute, which essentially adds the identifier provided as a suffix for @ + id / to the id resource file for other requests. The toolbar identifier can be defined as follows:

 android:id="@+id/toolbar 

The next identifier can now be tracked using findViewById (...), which looks for it in the res file for id or just in the R.id directory and returns the type of the View in question. The other, @id, behaves the same as findViewById (...) - it searches for a component by the provided id, but is reserved only for layouts. Its most common use is to place the component relative to the returned component.

 android:layout_below="@id/toolbar" 
0
Oct 18 '17 at 0:51
source share



All Articles