Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

layout direction not working in ConstraintLayout

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>
like image 392
Shakir Avatar asked Oct 24 '25 07:10

Shakir


2 Answers

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.

like image 99
Mohsin kazi Avatar answered Oct 25 '25 21:10

Mohsin kazi


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.

like image 40
صلي علي محمد Atef Farouk Avatar answered Oct 25 '25 22:10

صلي علي محمد Atef Farouk