Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference betwenn @id/ and @+id/ in android? [duplicate]

Tags:

android

Possible Duplicate:
What is different between @+id/android:list and @id/android:list ??

What is the difference between @id/.. and @+id/..? I am not referring to the difference between @android:id/.. and @id/..

Code Example:

<Button
android:id ="@id/add_button"
/>
<Button
android:id ="@+id/remove_button"
/>

What is the difference between the two id definitions above?

like image 708
hazem Avatar asked Apr 20 '11 13:04

hazem


People also ask

What's the difference between @ID and @+ id?

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

Is Android ID and device ID same?

All smartphones and tablets are identified by a unique device ID. The Android unique device ID is called the Android Advertising ID (AAID). It's an anonymized string of numbers and letters generated for the device upon initial setup. None of the user's personal information is included in an Android ID.

What is the use of ID in Android?

android:id. Resource ID. A unique resource name for the element, which you can use to obtain a reference to the View from your application.

What is id in XML Android?

ID resource is a simple resource defined in XML that provides a unique identifier for other project resources. An ID resource does not always reference an actual resource item, it is simply a unique ID that you can attach to other project resources or use as a unique integer in your app.


3 Answers

You must use the @+ notation on the first occurrence of the ID within an XML file. The second and subsequent times you can -- and should -- drop off the + sign. This will help catch typos.

For example, suppose that you have a RelativeLayout. You have a TextView in that RelativeLayout whose android:id is @+id/label. Later on in the layout XML file, you want to refer to that TextView from another one for positioning purposes (e.g., for android:layout_below).

If you typed in android:layout_below="@+id/labbel" (note the typo), at compile time, this will be considered OK. However, at runtime, things will not work, ranging from the widget being positioned incorrectly to an outright crash, depending on Android version.

If you typed in android:layout_below="@id/labbel" (note the typo and the missing + sign), then you would get a compile error.


UPDATE

Since I wasn't clear enough the first time, apparently, let's try again.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <TextView android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="URL:"
        android:layout_alignBaseline="@+id/entry"
        android:layout_alignParentLeft="true"/>
    <EditText
        android:id="@id/entry"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/label"
        android:layout_alignParentTop="true"/>
    <Button
        android:id="@+id/ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/entry"
        android:layout_alignRight="@id/entry"
        android:text="OK" />
    <Button
        android:id="@+id/cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/ok"
        android:layout_alignTop="@id/ok"
        android:text="Cancel" />
</RelativeLayout>

Above, you will see a RelativeLayout. You will notice that the first occurrences of each ID get the + sign. The second and subsequent occurrences of each ID do not get the + sign.

You could use the + sign on all of them, but then if you make a typo, the compiler will not catch the problem.

The + sign effectively states "allocate a new ID". Not having the + sign states "use a previously-allocated ID, or fail at compile time if there is no such ID".

like image 188
CommonsWare Avatar answered Oct 11 '22 16:10

CommonsWare


In the Android layout resource XML source files :

"@+id/anyId" : to add new id

"@id/anyId" : to refer existing id

You should use "@id/anyId" only when "anyId" is already added to R.java class using "@+id/anyId"

like image 36
Vaibhav Jani Avatar answered Oct 11 '22 15:10

Vaibhav Jani


From Android Guide

For the ID value, you should usually use this syntax form: "@+id/name". The plus symbol, +, indicates that this is a new resource ID and the aapt tool will create a new resource integer in the R.java class, if it doesn't already exist.

So + is for assigning a new id, it will also work when using existed id but it is not necessary there.

like image 21
MByD Avatar answered Oct 11 '22 16:10

MByD