Search code examples
androidandroid-layoutandroid-relativelayout

RTL issue on RelativeLayout Android


I am trying to add RTL to a relative layout.

This is how it's shown in LTR Layout.

LTR View

<?xml version="1.0" encoding="UTF-8"?><!-- $Id:  $ -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >


    <RelativeLayout
        android:id="@+id/preview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_centerInParent="true"
        android:paddingBottom="@dimen/dp16"
        android:paddingStart="@dimen/dp16"
        android:paddingEnd="@dimen/dp16"
        android:paddingTop="@dimen/dp16">

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/preview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="false"
            android:background="@drawable/ic_action_add"
            />
    </RelativeLayout>

    <TextView
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_gravity="center_vertical"
        android:layout_toStartOf="@+id/preview"
        android:ellipsize="end"
        android:lines="1"
        android:paddingBottom="14dp"
        android:paddingStart="@dimen/dp16"
        android:paddingEnd="@dimen/dp16"
        android:paddingTop="14dp"
        android:singleLine="true"
        android:text="Some text could be added here"
        android:textColor="@color/random_color"
        android:textSize="@dimen/text_medium" />

</RelativeLayout>

But when I try RTL, the layout is being shown as this:

RTL Layout

The text should be right aligned, but it's not getting right aligned.

I can do this programmatically by checking the layout direction and if it's RTL, then I could just put gravity to right and everything works fine. But is there any other option to do this in the xml layout itself.


Solution

  • try with android:textAlignment="viewStart" for this TextView

    may be also needed adding android:supportsRtl="true" to <application tag in manifest and its also possible thats you won't need textAligment for every TextView then

    android:supportsRtl doc:

    Declares whether your application is willing to support right-to-left (RTL) layouts. If set to true and targetSdkVersion is set to 17 or higher, various RTL APIs will be activated and used by the system so your app can display RTL layouts. If set to false or if targetSdkVersion is set to 16 or lower, the RTL APIs will be ignored or will have no effect and your app will behave the same regardless of the layout direction associated to the user's Locale choice (your layouts will always be left-to-right).

    The default value of this attribute is false.