Skip to content Skip to sidebar Skip to footer

Align To Right Side Of Relative Layout Does Not Seem To Work

I am trying to have 2 textviews one next to the other to the right side of the parent container. Something like: +++++++++++++++++++++++++++++++++++++ (parent container) |

Solution 1:

On your second TextView, use android:layout_toLeftOf="@id/textView1" and remove android:layout_alignParentTop="true" and android:layout_alignParentRight="true"

Like this:

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="100dp"android:layout_height="match_parent"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"
        ><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_alignParentTop="true"android:text="TEXTVIEW-1"
            /><TextViewandroid:id="@+id/textView2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_toLeftOf="@id/textView1"android:layout_marginRight="1dp"android:text="TEXTVIEW-2"
            /></RelativeLayout></LinearLayout>

What it actually does is:

When you used android:alignParentRight="true" in both of the textViews, It will place both the textViews to the right side overlapping each other.

So on adding it to only one textView it will align it to the right side only. And then adding toLeftOf will place it to the left of first textView.

UPDATE:

I didn't find any documentation for the question how is the priority given to RelativeLayout. But, I dig into the RelativeLayout.java file to find out. It was a bit hard to understand.

So I tried to play with the options and what i found :

  • If you apply android:layout_alignParentStart="true" and android:layout_alignParentEnd="true" the priority will be given to android:layout_alignParentStart

  • If you apply android:layout_toStartOf="@id/view2" and android:layout_toEndOf="@id/view2" the priority will be given to android:layout_toEndOf

  • If you apply android:layout_alignParentStart="true" and android:layout_toStartOf="@id/view1" the priority will be given to android:layout_alignParentStart

  • If you apply android:layout_alignParentStart="true" , android:layout_centerHorizontal="true" and android:layout_toStartOf="@id/view1"

    Then the priority will be given in order

     android:layout_alignParentStart="true"//1 High
     android:layout_toStartOf="@id/view1"//2 Medium
     android:layout_centerHorizontal="true"//3 Low

    So, it means android:layout_centerHorizontal will work only if android:layout_alignParentStart and android:layout_toStartOf will not be there.

Hope it is clear now. I would love to read about the priorities if someone finds the documentation of this.

Solution 2:

  android:layout_alignParentRight="true"

Remove this from textView2

Post a Comment for "Align To Right Side Of Relative Layout Does Not Seem To Work"