1

In my multi-lingual app, I handle text direction of a TextView based on the first character to be either RTL or LTR. Then the gravity would be set accordingly, too. What I want is different lines of a single TextView to have their own text directions and/or gravity.

Multi directional Text View

How should I achieve this?

Update:

As babadaba answered, TEXT_DIRECTION_ANY_RTL does exactly like what I want. Here is the explanation in comments:

Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains any strong RTL character, otherwise it is LTR if it contains any strong LTR characters. If there are neither, the paragraph direction is the view's resolved layout direction.

Now, the question is how to implement ANY_RTL in API levels older that 17 ?

EmJiHash
  • 623
  • 9
  • 22

1 Answers1

2

Create your Textview like this:

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="مرحبا \n Hello \n مرحبا"
    android:textDirection="anyRtl" />

It should check for each line if it has strong Arabic or Non-Arabic letters and format the line accordingly.

For older API Levels, you can try manually adding ‏‏the Unicode \u200F to your lines where you want RTL layout.

Shahood ul Hassan
  • 745
  • 2
  • 9
  • 19
babadaba
  • 814
  • 5
  • 20
  • It works. The only point is that the attribute textDirection is only used in API level 17+. any workaround for older ones? – EmJiHash Jul 02 '16 at 11:59
  • Check my edit, I provided a solution for older API Levels. Try that and report if it works for you :) – babadaba Jul 02 '16 at 12:06
  • It's not that I know which line should to be RTL or not! It's completely based on the content provided by users. It may be completely in an LTR language or fully in RTL. Or it might be a combination of both in separate lines, which is our problem here... – EmJiHash Jul 02 '16 at 12:27
  • How does the user provide the content? Via an EditText? If you can tell me how you get the String I may can write a little function which checks the String line by line and adds the unicode. – babadaba Jul 02 '16 at 12:45
  • You can consider the string as the input parameter of your function. – EmJiHash Jul 02 '16 at 12:54