Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New resource id declaration without the plus sign

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?

like image 345
user1506104 Avatar asked Jun 21 '18 06:06

user1506104


1 Answers

@+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

like image 151
Vikasdeep Singh Avatar answered Sep 19 '22 19:09

Vikasdeep Singh