Search code examples
androidtextview

How to fix textDirection for numeric TextView Android?


I have a text view which need to display a numeric code eg: 1234.

This works fine when the language is any left to right language like English. But when I switch to a right to left language like arabic the text field starts displaying 4321. The way to fix this is by textDirection='ltr (or locale)', but this also changes the text view gravity to the other direction.

Is there an elegant way to change the textDirection but not effect the gravity of the text field ? Or do I have to do a code check and flip gravity also or reverse the text if its numeric.

Please suggest the right way to fix this.


Solution

  • Put your TextView with textDirection='ltr' and layout_width='wrap_content' inside of LinearLayout with layout_width='match_parent' with gravity='start' or maybe gravity='end'. This make your Textview stay correct because of fixed direction while gravity will change by phone system language.