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 ?
- 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. stackoverflow.com/questions/6302221/…Yevgeny Simkin– Yevgeny Simkin2012-06-09 20:52:58 +00:00Commented Jun 9, 2012 at 20:52
- 2how to set Try adding a RIGHT-TO-LEFT MARK character (\u200F) ?? such this >> tv.setText("\u200F My Arabic text");Adham– Adham2012-06-09 21:08:37 +00:00Commented Jun 9, 2012 at 21:08
- I think so, as I said, I hadn't tried it, but that does look like the correct approachYevgeny Simkin– Yevgeny Simkin2012-06-09 21:16:14 +00:00Commented Jun 9, 2012 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.
1 Comment
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" 2 Comments
Attribute "textDirection" is only used in API level 17 and higher (current min is 13)android:textAlignment="viewStart"Too late , but the android:gravity="right" worked.
2 Comments
set this line in xml for textview :
android:textDirection="locale" 3 Comments
Try using
textview.setTextDirection(View.TEXT_DIRECTION_RTL); or
textview.setTextDirection(View.TEXT_DIRECTION_ANY_RTL); 1 Comment
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 Comment
android:textDirection="anyRtl" instead of using simply rtl additionally to your suggestion.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.
Comments
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"/> 1 Comment
layoutDirection ?