Reputation: 7247
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!
Upvotes: 12
Views: 10409
Reputation: 16976
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.
Upvotes: 20