I want to write text in (right to left language i.e. Arabic) in a TextView. But I want to make the text writing direction from right to left. gravity:right
will align the text to right only. I want to justify the text from right to left ( to make the words and numbers appear in he entered order in the line ) . how ?

- 37,646
- 24
- 106
- 138

- 63,550
- 98
- 229
- 344
-
1I believe this answers your question. It's marked as correct... I haven't tested it to verify that it is *indeed correct, but it looks like it should work. http://stackoverflow.com/questions/6302221/android-setting-problem-with-textview-for-hebrew-text – Yevgeny Simkin Jun 09 '12 at 20:52
-
2how to set Try adding a RIGHT-TO-LEFT MARK character (\u200F) ?? such this >> tv.setText("\u200F My Arabic text"); – Adham Jun 09 '12 at 21:08
-
I think so, as I said, I hadn't tried it, but that does look like the correct approach – Yevgeny Simkin Jun 09 '12 at 21:16
12 Answers
Another clever way which can be used in older versions of android and can be used anywhere in string so it's handier even in latest version of android, is to include the right-to-left mark or even left-to-right mark in the string when it's needed:
left-to-right mark: ‎ or ‎ (U+200E)
right-to-left mark: ‏ or ‏ (U+200F)
This is how it's down:
String rtl = "Hello \u200F(سلام)";
The good thing about this method is you can even use it after punctuation like (,{,[,! which are positioned right incorrectly! examples:
سلام! // wrong position
سلام! // right position by adding RLM after !
look how ! is positioned correctly in second line, actually there is an RLM after it, and you can copy paste it even if it's invisible.
The second good thing as you saw it is that you can use this method (these char codes) in any system that supports RTL like browsers, word and so on.

- 21,572
- 15
- 83
- 95
-
2@SaeedNeamati These chars are added to UTF exactly for this purpose, so as long as rendering engine respect them there will be no side effect, which is the case in android. The only thing is, in some fonts a representation glyph exist for these codes, and they are displayed when these chars are used. you must remove their glyphs using some font editor tools like font creator. – Ali Sep 08 '15 at 12:14
-
1
Just put this property in your TextView and everything will workout just fine it will automatically detect your text and change accordingly
android:textDirection="anyRtl"

- 719
- 1
- 7
- 10
-
5`Attribute "textDirection" is only used in API level 17 and higher (current min is 13)` – Iman Marashi Oct 18 '15 at 19:55
-
8
Too late , but the android:gravity="right"
worked.

- 1,849
- 7
- 29
- 46
-
1this will mess up mixed text, numbering, braces etc. definitely use android:gravity="right" /see: http://android-developers.blogspot.co.il/2013/03/native-rtl-support-in-android-42.html – Assaf S. Mar 04 '15 at 09:42
set this line in xml for textview :
android:textDirection="locale"

- 722
- 9
- 23

- 673
- 8
- 11
-
and sets text as for Arabic ...myTextView.settext("\u200F"+"text"). – arshad shaikh Mar 01 '17 at 05:45
-
-
For me, all Arabic texts were showing right to left properly. But when the text started with an English word (rest of the text was Arabic), it was showing left to right.. android:textDirection="locale" -> this line solved it for me. Thank you!! – Nayan Apr 11 '23 at 05:45
Try using
textview.setTextDirection(View.TEXT_DIRECTION_RTL);
or
textview.setTextDirection(View.TEXT_DIRECTION_ANY_RTL);

- 5,593
- 38
- 51
In case of normal edit text this will work
android:textDirection="rtl"
But in case of password fields this is not enough then follow this;
android:textDirection="rtl"
android:gravity="right"

- 1,125
- 3
- 13
- 25
-
2For me it was crucial using `android:textDirection="anyRtl"` instead of using simply `rtl` additionally to your suggestion. – antonis_st Jun 16 '17 at 08:03
Use android:layoutDirection="rtl"
and android:textAlignment="viewStart"
to make the text view right-to-left.

- 2,397
- 2
- 21
- 31
Add these to your editText:
android:gravity="right"
android:textDirection="rtl"
PS: android:textDirection
requires API level 17

- 6,628
- 2
- 35
- 56

- 1,399
- 1
- 15
- 36
just adding my own experience with such issues. in my case in addition to suggested solutions i also had to replace English " character with the RTL (in my case hebrew) representation of such character ״

- 1,190
- 1
- 9
- 16
By using attributes as below in my xml code strangely I got a right to left text with correct punctuation. But this is not a perfect solution for multi language text:
android:textDirection="ltr"
android:gravity="right"
setting ltr corrects punctuation problems like having dot on right side instead of left.
gravity attribute makes text flow from right to left.

- 84
- 6
best way textDirection for TextView, Don't use layoutDirection
<TextView
android:id="@+id/viewCountTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textDirection="anyRtl"
android:gravity="center"/>

- 11,234
- 1
- 68
- 78
-
Can you provide more info in why not to use `layoutDirection` ? – Juan M. Rivero Nov 06 '19 at 16:11