Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android RTL Text Direction

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

enter image description here

Test In English RTL Forced

enter image description here

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

enter image description here

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!

like image 441
Ersen Osman Avatar asked Sep 26 '18 19:09

Ersen Osman


People also ask

How do I change RTL on android?

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.

What is RTL layout direction?

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.

What is LTR and RTL in android?

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).

How do you write vertically on android?

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.


1 Answers

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.

like image 51
ʍѳђઽ૯ท Avatar answered Sep 20 '22 06:09

ʍѳђઽ૯ท