How to make the text direction from right to left
AndroidTextviewRight to-LeftAndroid Problem Overview
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 ?
Android Solutions
Solution 1 - Android
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.
Solution 2 - Android
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"
Solution 3 - Android
Too late , but the android:gravity="right"
worked.
Solution 4 - Android
set this line in xml for textview :
android:textDirection="locale"
Solution 5 - Android
Try using
textview.setTextDirection(View.TEXT_DIRECTION_RTL);
or
textview.setTextDirection(View.TEXT_DIRECTION_ANY_RTL);
Solution 6 - Android
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"
Solution 7 - Android
Use android:layoutDirection="rtl"
and android:textAlignment="viewStart"
to make the text view right-to-left.
Solution 8 - Android
try this as it worked for me android:textDirection="rtl"
Solution 9 - Android
Add these to your editText:
android:gravity="right"
android:textDirection="rtl"
PS: android:textDirection
requires API level 17
Solution 10 - Android
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 ״
Solution 11 - Android
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.
Solution 12 - Android
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"/>