I am attempting support RTL locales in my application however I ran into the following issue. Now I say issue but it might not even be an issue due to my unfamiliarity of RTL locales so here it goes.
Below are 3 text views. Note the following outputs have been made with RTL forced on via the dev option.
Test In English LTR
Test In English RTL Forced
I found it strange that it didn't align to the right even though I said android:gravity="start"
like how it works with padding and margin etc. so I looked at other answers and came across android:textDirection="locale"
.
Test In English RTL Forced Using Text Direction Locale
Now this is mirroring correctly on both LTR and RTL but then I noticed the following behaviour with the punctuation. This raised a eyebrow whether or not this is correct because I was comparing with Google Play Store, Facebook and Whats App with RTL forced and I did not see this when looking at paragraphs with punctuation which makes me believe I did something wrong.
Full Layout Code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="start"
android:text="@string/test_one"
android:textColor="@android:color/black"
android:textDirection="locale"
android:textSize="24sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="start"
android:text="@string/test_two"
android:textColor="@android:color/black"
android:textDirection="locale"
android:textSize="24sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="start"
android:text="@string/test_three"
android:textColor="@android:color/black"
android:textDirection="locale"
android:textSize="24sp" />
</LinearLayout>
Other code
The following is set in my manifest file
android:supportsRtl="true"
And this is my strings
<string name="test_one">Good Job. You did well!</string>
<string name="test_two">3 hours ago</string>
<string name="test_three">Select Option:</string>
Please forgive how naive this might be sounding and enlighten me! I wish to provide comfortable RTL support!
Just go to Android Studio > Refactor > Add RTL support where possible… I would recommend you checking your app once after applying this change as you might not want all your Layouts/Views to be RTL. If you want to force any layout to LTR then just add android:layoutDirection="ltr" to that view.
When text, layout, and iconography are mirrored to support right-to-left (RTL) UIs, anything that relates to time should be depicted as moving from right to left. For example, in a RTL layout, forward points to the left, and backwards points to the right.
So when you setted an RTL script text to textview, text will start from right automatically. But if in RTL mode you set an LTR script text, textView's text will start left like below (Even if you replaced left/right to start/end).
The actual rotation is done by overriding onDraw function. Inside onDraw, we have to save the canvas being drawn, then translate the canvas to our measured width and height. Now rotate this canvas at 270f angle to make it vertical.
Adding android:textDirection="locale"
to the TextView
and android:supportsRtl="true"
will do the trick. However, textDirection="locale"
changes the direction in order to locale which is en
or fa
or it is ar
. (Depends on device's language)
And to be clear enough, it's working properly because the text you have in the TextView
is LTR
and you used RTL
direction which as you can see, it made the direction RTL
and there is no problem with that. Also, using android:gravity="start"
will probably force it to use from start which I suppose, it will start from LTR
. (I'd suggest removing android:gravity="start"
and let the Android(In order to device's language) decide which one is the best)
So, to test if it is working or not, you can set the text to arabic or persian language as follows:
android:text="این یک تست است"
If it started from RTL
and with "این"
, so it works as expected.
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