Search code examples
androidandroid-layoutandroid-relativelayout

RelativeLayout View to right of TextView


I got following simple Layout. The problem can be reproduced in the android studio designer:

<?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">

<TextView
    android:id="@+id/x"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_marginEnd="@dimen/margin_small"
    android:layout_marginRight="@dimen/margin_small"
    android:text="@string/long_string"
    android:textAppearance="?android:attr/textAppearanceSmall"/>
<CheckBox
    android:id="@+id/y"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/x"
    android:layout_toEndOf="@+id/x"
    android:layout_centerVertical="true" />

</RelativeLayout>

This layout works fine if the text length of the textview is short. The checkbox is placed on the right of the textview. But if the text gets long and even wraps maybe, then the checkbox is pushed out of the view. It is not visible anymore. I would like that the checkbox is always visible on the right of the textview even, if it fills the whole width of the screen.

I tried to rewrite the layout with a LinearLayout which doesn't work either.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

<TextView
    android:id="@+id/x"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="@dimen/margin_small"
    android:layout_marginRight="@dimen/margin_small"
    android:text="@string/long_string"
    android:textAppearance="?android:attr/textAppearanceSmall"/>
<CheckBox
    android:id="@+id/y"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
   />

 </LinearLayout>

Do you know a trick to to achieve this with relative layout? I would somehow expect this behaviour from relative layout by default. Thanks ;)


Solution

  • This is working for me: make checkBox alignParentRight and make TextView toLeftOf it:

    <?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">
    
        <TextView
            android:id="@+id/x"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@+id/y"
            android:text="This is very-very-very looooooooooooong stringgggg, very-very, long-long"
            android:textAppearance="?android:attr/textAppearanceSmall"/>
        <CheckBox
            android:id="@+id/y"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"/>
    
    </RelativeLayout>
    

    Edit. You can include this Relative Layout into other (parent) layout:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
    
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:gravity="left">
    
            <TextView
                android:id="@+id/x"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_toLeftOf="@+id/y"
                android:text="this is veryyyyy yyyyyyyyyy yyyyyy yyy loooooo oooooooo ooon nnggggg gggg striiii iiiiin gggggg ggggg ggggggg ggg"
                android:textAppearance="?android:attr/textAppearanceSmall"/>
            <CheckBox
                android:id="@+id/y"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:layout_centerVertical="true" />    
    
        </RelativeLayout>
    
    </RelativeLayout>
    

    It's also working. If you put android:gravity="left" into Relative layout, it will locate its content on the left side.