Search code examples
androidright-to-left

android : is constraint layout support RTL


what is the best practice to support RTL in constraint layout in android Studio,
or should I create A separated layouts one for English and the other for Arabic?

English Version

English version

The Expected layout with Arabic language The Expected layout with Arabic language

The output layout when I change the Device Language from English to Arabic The output layout when Change the Language from English to Arabic

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:text="@string/CourseName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView7"
        android:layout_marginTop="16dp"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginStart="16dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginLeft="16dp"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1" />

    <Button
        android:text="@string/enroll"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:layout_marginTop="16dp"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginEnd="16dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginRight="16dp"
        android:textAppearance="@style/TextAppearance.AppCompat.Display2" />
</android.support.constraint.ConstraintLayout>

Solution

  • As Already pointed out by CommonsWare, you should rarely/never use Left/Right if you plan on supporting RTL languages, especially if you target API 16+.

    Replace your app:layout_constraintRight_toRightOf="parent" with app:layout_constraintEnd_toEndOf="parent" and so forth.

    End for Right, Start for Left.