portfoliobuilder
portfoliobuilder

Reputation: 7866

In XML how to align two elements (or widgets) to the right w/ Relative Layout?

Lets say you have two buttons side by side under Relative Layout like

<Button
    android:id="@+id/arrow_up"
    android:layout_width="40dp"
    android:layout_height="40dp" />

<Button
    android:id="@+id/arrow_down"
    android:layout_width="40dp"
    android:layout_height="40dp" /> 

I have tried adding the attribute android:layout_alignParentRight="true" to both and there are major issues. First, if you add it to button 1 (id:arrow_up) and not to button 2 (id:arrow_down), the second button disappears because button 1 moves all the way to the right. Second, if you add it to button 2 and not button 1, then button 2 will be stretched until it reaches all the way to the right side and ignores all and any width values (40dp as in this example). Third, if you add it to both, the result is as described when you have the alightParentRight attribute under button 1 and not button 2.

So how is this done? How to align both buttons to the right, side by side and maintain the proper assigned width values?

Upvotes: 0

Views: 3659

Answers (1)

Orphamiel
Orphamiel

Reputation: 884

<Button
    android:id="@+id/button2"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_alignParentRight="true"/>

<Button
    android:id="@+id/button1"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_toLeftOf="@id/button2" />

Upvotes: 2

Related Questions