Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

issue with "@id" vs "@+id" in positioning in Relative Layout

Tags:

android

Good day, i have a slight issue with relative layouts and i don't know why. Normally when you trying to position relative to other views, you use "@id" but it doesn't seem to position at all. only when i use the value "@+id" would it go correctly. in the example below, i have 4 views in a horizontal orientation i want the TextView with "percentage_id" to be positioned in between the imageviews but closer to the last one.

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/sales_id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="10dp"
        android:text="£0.00"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#AADDEE"
        android:textSize="18sp"
        android:textStyle="bold" >
    </TextView>

    <ImageView
        android:id="@+id/arrow_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_toRightOf="@id/sales_id"
        android:baselineAlignBottom="true"
        android:src="@drawable/rightarrow" >
    </ImageView>

    <TextView
        android:id="@+id/percentage_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/imagearrow_id"
        android:layout_toRightOf="@id/arrow_id"
        android:text="0.00%"
        android:textColor="#606090"
        android:textSize="18sp" >
    </TextView>

    <ImageView
        android:id="@+id/imagearrow_id"
        android:layout_width="20dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="2dp"
        android:layout_weight="0"
        android:gravity="right"
        android:padding="5dp"
        android:src="@drawable/rightarrow" >
    </ImageView>

</RelativeLayout>

Now no matter what i do, it just goes to the default position in the relative layout and only when i use "@+id" instead of "@id" does it go to the correct position. I know sometimes it can give error if the view am trying to reference has not been declared yet but even if i place the textview last, i still can't get it where i want until i use "@+id".

is this is a new thing that works also with Relative Layout? because i have no idea why its not working with "@id". am i fine this way? anyone encountered the same issue? see some tutorials on the net use "@+id" for positioning too. Thank you

like image 545
irobotxx Avatar asked Oct 09 '12 17:10

irobotxx


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 Relative layout deprecated?

@filthy_wizard: RelativeLayout is not deprecated.

What is ID layout?

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

Which type of layout allow the component in relative position?

RelativeLayout lets child views specify their position relative to the parent view or to each other (specified by ID). So you can align two elements by right border, or make one below another, centered in the screen, centered left, and so on.


1 Answers

Although you can refer to views declared later in the hierarchy, the XML document is still parsed sequentially. The first time that you refer to a particular ID, you have to prefix it with @+id. In actuality, you can prefix every reference to an ID with @+id and everything will work -- in fact, this is what happens if you design your interface with the graphical editor. Just to be safe, everything is prefixed with @+id. The + simply tells it to generate an ID in R.java if and only if it has not already been defined. If you try to define it more than once, it just sees that it's already been defined and continues on normally.

In your XML, you reference imagearrow_id from your percentage_id TextView. However, at this point the imagearrow_id has not yet been defined. For this scenario, you could simply prefix the layout_toLeftOf=@+id/imagearrow_id in your TextView, and then below, when defining your ImageView (imagearrow_id), you would not need the + in the android:id attribute.

like image 188
Kevin Coppock Avatar answered Sep 28 '22 15:09

Kevin Coppock