9

This documentation always puzzles me:

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. For example:

<TextView android:id="@+id/nameTextbox"/>

I've been programming for quite a while now. However, I've never encountered any case wherein I have to use the ID declaration without the plus sign. It is also counter-intuitive. IDs are supposed to be unique!

Any good use-case for this? Why would one want to re-use the same resource id name?

user1506104
  • 6,554
  • 4
  • 71
  • 89

4 Answers4

7

@+id/name When you create a new id

"@id/" When you link to existing id

Use-case example 1:

Let's say you created your own resource in XML:

<resources>
    <item name="plusIcon" type="id"/>
</resources>

Now you can use this resource at multiple places without creating a new resource using @+id. Say layout_one.xml:

<TextView android:id="@id/plusIcon"/>

Same resource in layout_two.xml:

<TextView android:id="@id/plusIcon"/>

Use-case example 2:

There are a number of other ID resources that are offered by the Android framework. If you want to referencing an Android resource ID in that case you can use @android you don't need to create your own new resource ID

Vikasdeep Singh
  • 20,983
  • 15
  • 78
  • 104
5

It means if you have declared a view in a layout_one.xml like

<TextView
    android:text="Sample Text"
    android:id="@+id/text_view_sample"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

And if you have similar textView into layout_two.xml like

<TextView
    android:text="Sample Text2"
    android:id="@+id/text_view_sample"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

In both cases there will be only one id created into R.java and it will be reused into another xml (whichever will be called after).

SO here you can live with (in layout_two.xml)

<TextView
        android:text="Sample Text2"
        android:id="@id/text_view_sample"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

here Android will reuse id as it was created before from layout_one.xml.

You should also read Difference between "@id/" and "@+id/" in Android and What is the difference between @id and @+id?

Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
  • 1
    Please read the question. Asking about when to use **without +** – Vikasdeep Singh Jun 21 '18 at 06:27
  • @VicJordan And what makes you to think that it is not answering the question? – Pankaj Kumar Jun 21 '18 at 06:29
  • @VicJordan :D So this is lession for you to wait for sometime. Someone may update answer. By the way I just added few links not edited my answer. – Pankaj Kumar Jun 21 '18 at 06:32
  • this is close to what im looking for. but why would one want to use same id for two textviews from different layouts? in OOP concepts, these two textviews are two different objects, thus calls for two different names. – user1506104 Jun 21 '18 at 06:45
  • @user1506104 Yes correct, they are two different objects and must have different id. This is the reason you can not use more than one view with same id in a layout. Now talking about two different layouts, they have different scope and you have option to keep views with different ids, but if there is no chance of collision of ids for two different layouts, why should I declare another id for similar view. It promotes to reuse layouts also – Pankaj Kumar Jun 21 '18 at 06:52
  • @user1506104 Assume I have two screens both have a part which is common. I have two ways either design that part into both layout or either design that part separately and include that into both. While considering first way, you have option to change ids (for views which are in common part), but that is not reusable. When you talk about reusable view you will go with second option BUT you will have same ids for all views which are in that layout. Think about if I would not have way to keep view with same id in two different layouts, how this reusability could be achieved. – Pankaj Kumar Jun 21 '18 at 06:59
  • @user1506104 I did not find best way to explain it, but hope few things I cleared. – Pankaj Kumar Jun 21 '18 at 06:59
  • @PankajKumar you may have a point there. you might want to translate that into code? :) Correction: you can have two textviews in one layout with same ID. 1st textview declared with + sigh while 2nd textview should be declared withtout the + sign. (upvoting though) – user1506104 Jun 21 '18 at 07:09
  • 3
    @user1506104 what you have explained is exactly what I did in my answer. – Vikasdeep Singh Jun 21 '18 at 07:18
  • @user1506104 great finding. Its allowing same id in same file also, where findView returns first view whichever would declared first in view group (As what findView works). – Pankaj Kumar Jun 21 '18 at 07:34
  • @Pankaj Kumar: What will happen if id not already exist, I haven't used + sign in id? – Jitesh Mohite Jun 25 '18 at 02:50
  • @jiteshmohite In that case lint will give you the error "can not resolve symbol ......". And you will not be able to run your code. – Pankaj Kumar Jun 25 '18 at 05:43
3

Defining the constraints within a RelativeLayout might be a good example.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:text="Top"/>

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Bottom"/>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/textView3"
        android:layout_below="@id/textView1"
        android:text="Center"/>

</RelativeLayout>

On the last TextView android:layout_above and android:layout_below don't require the plus symbol because textView1 and textView2 are already defined IDs.

Marco Lovetere
  • 211
  • 3
  • 5
3

Firstly

we use + when we're referencing a id for the first time(in top to down order) in a particular xml file, not when we create some id for the first time.

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent">
  <Button
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/another_button"
    android:layout_alignParentTop="true"
    android:text="@string/button" />
  <Button
    android:id="@id/another_button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:text="@string/another_button" />
 </RelativeLayout>

Secondly

when someone is working with RelativeLayout or ConstraintLayout i.e. some relative parent view they need to use same id multiple times in order to define the activity or some view in the activity etc.

Thirdly

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). So every time we use @+id/some_id, it triggers the creation of a new resource reference to the same view, i.e., redundant.

Example(for the second use case)

<RelativeLayout
    android:id="@+id/final_order_activity_order_rl"
    android:layout_margin="5dp"
    android:background="@drawable/gradient_for_btns"
    android:paddingBottom="8dp"
    android:paddingTop="8dp"
    android:paddingStart="4dp"
    android:paddingEnd="4dp"
    android:layout_alignParentBottom="true"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_centerInParent="true"
        android:layout_marginStart="8dp"
        android:textStyle="bold"
        android:textSize="18dp"
        android:text="$2000"
        android:textColor="@android:color/white"
        android:maxLines="1"
        android:layout_toLeftOf="@+id/final_order_activity_place_order_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/final_order_activity_total_tv" />

    <Button
        android:paddingStart="6dp"
        android:paddingEnd="6dp"
        android:layout_marginEnd="8dp"
        android:text="Place Order"
        android:background="@drawable/ripple_effect"
        android:textColor="@color/baseColorBright"
        android:layout_alignParentEnd="true"
        android:layout_width="wrap_content"
        android:layout_height="28dp"
        android:id="@id/final_order_activity_place_order_btn"/>

</RelativeLayout>
Bilal Ahmad
  • 781
  • 8
  • 10
  • Your first example is a good use-case. Can you expound on your second example pls? Looks the the answer im looking for. – user1506104 Jun 29 '18 at 20:51
  • First example also explains the second use-case. But I've added a new example, I hope this will work for you. – Bilal Ahmad Jun 30 '18 at 17:05