How to prevent automatic right-to-left text direction for Hebrew and Arabic?
Asked Answered
E

2

14

In a TextView, when a text starts with a Hebrew letter, the entire text is shown in RTL mode.

The problem is that in my case only the first word is in Hebrew, while the entire sentence is in English, so instead of

 דני went to school

the user sees

                                                  went to school דני

(the first word is in the end of the sentence, when reading in English)

How can I prevent this from happening and force the text to start from the left?

Ethelynethene answered 12/1, 2015 at 20:44 Comment(3)
I would say it is a matter of adding the right BiDi characters to indicate that the whole text is LTR with the exception of the small RTL part. It looks like the sentence is defaulted to RTL because it starts with RTL characters. To verify this, you can try to add an LTR sequence at the beginning of the text.Penneypenni
starting the text with \u200E could be sufficient to force the text to be considered LTR.Penneypenni
Thank you, @njzk2. if you make this an answer, I'll accept it.Ethelynethene
P
27

Unicode characters are intrisectly LTR or RTL. In most situations, the whole text takes its orientation from the first character. Hebrew character are RTL.

See http://en.wikipedia.org/wiki/Bi-directional_text

Your text starts with a RTL character, and is therefore considered RTL. You can force it to be seen as LTR by using the left-to-right mark, "\u200E".

Starting your text with this special character will tell the layout system the explicit orientation of the whole text. RTL sections will still be considered as such, though, because the characters are explicitly RTL.

Numbers, for example, are not a strong indicator, and therefore don't affect the direction of the section they are placed in. Therefore, numbers between section of different direction can be a problem, and may need explicit indication.

Penneypenni answered 12/1, 2015 at 23:34 Comment(3)
See also developer.android.com/reference/android/support/v4/text/…Tangle
It Works when using string resources, but not when from any database. (like SQL database)Subterfuge
Thx Saved my day man . This thing was a horror ..Bootery
M
1

As alternative you can use android:textDirection="locale", so for devices with LTR locales any string with RTL characters will be displayed properly.

Millan answered 14/5, 2021 at 15:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.