4

I want to use the same layout for Arabic and English using ConstraintLayout. When I am using LinearLayout, the ImageView and TextView horizontally aligned, and changes the layoutdirection with respect to language. But when I am using constraintlayout the image view and TextView not aligning from 'right to left' in Arabic Language.

layout preview screenshots

preview in english

preview in arabic

desired output in Arabic

But I am not getting this when using ConstraintLayout

<?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/rootView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:background="@color/white"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher_2"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:text="@string/work_name"
            app:layout_constraintBottom_toBottomOf="@+id/imageView"
            app:layout_constraintStart_toEndOf="@+id/imageView"
            app:layout_constraintTop_toTopOf="@+id/imageView" />

    </androidx.constraintlayout.widget.ConstraintLayout>
Shakir
  • 91
  • 1
  • 7

2 Answers2

2

Aligning text from right side for TextView

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:text="@string/work_name"
        android:gravity="center|right"
        app:layout_constraintBottom_toBottomOf="@+id/imageView"
        app:layout_constraintStart_toEndOf="@+id/imageView"
        app:layout_constraintTop_toTopOf="@+id/imageView" />

android:gravity="center|right"

set layout direction in coding

1)Find out TextViews id in onCreate()

2) follow below code

textview.setGravity(Gravity.CENTER|Gravity.RIGHT);
OR
textview.setGravity(Gravity.CENTER|Gravity.LEFT);

3) Apply as per your text.

I recommended you to use 2 Image view and 2 text view in constraint layout , when you want to show English text then hide Right side image and text and show Left side image and text for Arabic text make vise-versa.

Try it.

Mohsin kazi
  • 532
  • 1
  • 8
  • 15
0

If you want to keep Arabic layout same like English layout for the whole project then add this line Manifest:

    <application
    android:supportsRtl="false"

I would suggest that you make a separated XML file for each right to left language. noting that right to left languages short code like (-ar, -fa, -ps, -sd, -ur) , if you got more arabic to left codes, let us know.

  • Is it possible to have this flag set to false, yet in some places do allow to use RTL/LTR direction manually? – android developer Aug 18 '21 at 07:14
  • android:supportsRtl="false" is defined in the application level. once defined arabic/english languages will have same layout if you didn't define layout-ar – صلي علي محمد - Atef Farouk Aug 18 '21 at 07:52
  • I don't understand. You mean that putting layout files in this folder, they won't be used? My question was about ConstraintLayout, though... – android developer Aug 18 '21 at 07:54
  • I mean "android:supportsRtl="false" is per application not per activity. and in this case if you change language between arabic/english, then no difference. the solution is to use layout-ar and in this case you are free to re arrange component in the layout as per your request. – صلي علي محمد - Atef Farouk Aug 18 '21 at 10:46
  • Can't I let `"android:supportsRtl="false" ` stay, and fix one layout after another, without affecting the rest (as they could have issues) ? – android developer Aug 18 '21 at 11:19
  • Sure, this is best solution. having "android:supportsRtl="false" will give you same layout behavior for both Eglish/Arabic language. fix each activity by adding layout-ar and change in this layout your desire directions. don't forget to copy layout-ar to other right to left languages like (fa, -ps, -sd, -ur) if needed. – صلي علي محمد - Atef Farouk Aug 19 '21 at 04:00
  • You can put in the folder of RTL instead of a specific language. As for a solution, I think I asked you the question in a wrong way. Here: https://stackoverflow.com/q/68828479/878126 – android developer Aug 20 '21 at 05:19