Issue
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?
Solution
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>
Answered By - Bilal Ahmad
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.