JuiCe JuiCe - 24 days ago 10
Android Question

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

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.

Answer

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.

And now i add for you 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.

Comments