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>
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With