68

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:rightwill 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 ?

chubbsondubs
  • 37,646
  • 24
  • 106
  • 138
Adham
  • 63,550
  • 98
  • 229
  • 344
  • 1
    I 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
  • 2
    how 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 Answers12

67

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.

Ali
  • 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
    life-saving. Thanks – Syeda Zunaira Mar 08 '18 at 10:55
52

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"
RaedMarji
  • 719
  • 1
  • 7
  • 10
21

Too late , but the android:gravity="right" worked.

losingsleeep
  • 1,849
  • 7
  • 29
  • 46
  • 1
    this 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
19

set this line in xml for textview :

 android:textDirection="locale"
Farshid roohi
  • 722
  • 9
  • 23
arshad shaikh
  • 673
  • 8
  • 11
  • and sets text as for Arabic ...myTextView.settext("\u200F"+"text"). – arshad shaikh Mar 01 '17 at 05:45
  • I have seperate xml layout for arabic designs – arshad shaikh Oct 25 '18 at 12:23
  • 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
16

Try using

 textview.setTextDirection(View.TEXT_DIRECTION_RTL);

or

 textview.setTextDirection(View.TEXT_DIRECTION_ANY_RTL);
Iman Marashi
  • 5,593
  • 38
  • 51
7

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"
manDroid
  • 1,125
  • 3
  • 13
  • 25
  • 2
    For 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
7

Use android:layoutDirection="rtl" and android:textAlignment="viewStart" to make the text view right-to-left.

Armin
  • 2,397
  • 2
  • 21
  • 31
3

try this as it worked for me android:textDirection="rtl"

SMR
  • 6,628
  • 2
  • 35
  • 56
user2876982
  • 57
  • 2
  • 4
3

Add these to your editText:

android:gravity="right"
android:textDirection="rtl"

PS: android:textDirection requires API level 17

SMR
  • 6,628
  • 2
  • 35
  • 56
Kaaveh Mohamedi
  • 1,399
  • 1
  • 15
  • 36
0

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 ״

Liran Cohen
  • 1,190
  • 1
  • 9
  • 16
0

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.

Lord Tesla
  • 84
  • 6
0

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"/>
Rasoul Miri
  • 11,234
  • 1
  • 68
  • 78