Search code examples
androidandroid-relativelayout

Two TextViews inside RelativeLayout with proper respond to rtl locale


How can I create 2 TextViews in a RelativeLayout that look like this in LTR and RTL locales.

(Table is just for clearing what I mean, it's not part of my question)

LTR

╔════════════╦═══════════════════════════════════════════════════════════╗
║  txtView1  ║                        txtView2                           ║
╚════════════╩═══════════════════════════════════════════════════════════╝

RTL

╔═════════════════════════════════════════════════════════════╦══════════╗
║                           txtView2                          ║ txtView1 ║
╚═════════════════════════════════════════════════════════════╩══════════╝

currently I'm using margins but this doesn't seem right:

<RelativeLayout
    android:id="@+id/title_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/txtView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView 1"
        android:textColor="?attr/info_color"
        android:textSize="13sp" />

    <TextView
        android:id="@+id/txtView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/txtView1"
        android:text="TextView 2"
        android:layout_marginLeft="60dp"
        android:layout_marginRight="60dp" />

</RelativeLayout>

I can't use setLayoutDirection, minSdkVersion is below 17"


Solution

  • I'm not sure if this is what you want but you can:

    1 - Set android:layout_width to wrap_content in TextView1.

    2 - Align TextView1 to parent's start

    3 - Align the TextView2 at the end of TextView1

    <RelativeLayout
        android:id="@+id/title_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <TextView
            android:id="@+id/txtView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:text="TextView 1"
            android:textSize="13sp" />
    
        <TextView
            android:id="@+id/txtView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_alignBaseline="@id/txtView1"
            android:layout_toEndOf="@id/txtView1"
            android:text="TextView 2"/>
    </RelativeLayout>
    

    This is the result:

    LTR

    enter image description here

    RTL

    enter image description here

    EDIT

    As well mentioned in the comments, you can use a LinearLayout:

    <LinearLayout
        android:id="@+id/title_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="1">
    
        <TextView
            android:id="@+id/txtView1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="TextView 1"
            android:textSize="13sp"
            android:layout_weight="0.2"/>
    
        <TextView
            android:id="@+id/txtView2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_weight="0.8"
            android:layout_alignBaseline="@id/txtView1"
            android:text="TextView 2"/>
    </LinearLayout>