2

I'm working on a layout that includes both English and Hebrew (intended as right-to-left) text, in separate views. When a line of Hebrew text gets beyond a certain length, it becomes written left-to-right (I assume that this has to do with length because it looks fine with shorter text, and when I display it on a tablet instead of a phone). The relevant view looks like this:

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:gravity="center"
    android:textDirection="rtl"
    android:text="@string/tx_middle_text"
    android:textSize="@dimen/big_text_dp" />

and the string is defined in strings.xml like this:

<string name="tx_middle_text">טקסט בעברית</string>

(I've replaced the original text, which was 50 characters long, and made up entirely of Hebrew letters and white-spaces).
Note the text has the rtl attribute. I've tried replacing it with anyRtl, and I've tried changing gravity to "right" - neither helps.
I need the text to remain in one line and be cut off with an ellipsis if it doesn't fit - as it is, that's what happens, but with the text written left-to-right.
How can I fix this?

Edit: For an ad-hoc solution I made a shorter string as an alternative resource for the smaller layout (it works as long as the text is less than one line long on a given device), but I'd still like to know if there's a general solution to this.

MASQ
  • 135
  • 15
  • I've also tried adding the RIGHT-TO-LEFT MARK character (\u200F) at the beginning of the text, as suggested here: [Android setting with TextView for Hebrew text?](http://stackoverflow.com/questions/6302221/android-setting-with-textview-for-hebrew-text), which did not help. – MASQ Apr 08 '15 at 11:46
  • you should share your long string too for which it is causing you problem so that answerers can help you to find the exact cause. – Amit K. Saha Apr 08 '15 at 11:48
  • it's kinda private atm, but I've tried replacing it with different strings of similar length and had the same problem, whereas shortening it removed the problem. It's all Hebrew characters and spaces (no LTR characters or numbers to make things more complicated). – MASQ Apr 08 '15 at 14:03
  • change android:gravity="center" to android:gravity="right" – Amit K. Saha Apr 08 '15 at 14:07
  • Tried it, as mentioned in the question. – MASQ Apr 08 '15 at 14:20
  • I would suggest you to try removing android:singleLine="true" android:gravity="center both of them just to see if now text is shown properly – Amit K. Saha Apr 08 '15 at 16:27
  • Yes, it's shown properly when removing singleLine="true" (but then there's 2 lines of text on smaller devices, which messes with the layout). – MASQ Apr 13 '15 at 10:25
  • if removal singleLine="true" solves your problem, then you can try android:maxLines="1" to achieve that. – Amit K. Saha Apr 13 '15 at 11:18
  • Tried that, the answer below suggested it. Seems like I just found an edge case with the RTL handling or something - looks like it turns off when it has to cut off/ellipsize a long line where the lines are limited. – MASQ Apr 14 '15 at 12:00

2 Answers2

1

RTL is detected automatically by the system depending on the characters. i.e for Arabic characters, text will be drawn from right to left.

This is an example on how my spinner is drawn. For this TextView:

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/spinner_item"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:textSize="15sp"
    android:gravity="center_vertical|start"
    android:textColor="@color/spinner_filter_text"
    android:ellipsize="end"
    android:singleLine="true"
    android:paddingStart="20dp"
    android:paddingEnd="20dp"/>

And these strings:

<string name="all_categories">All Categories</string> (values)

enter image description here (values-ar)

The result is:

enter image description here enter image description here

If I modify the TextView with:

android:layout_width="100dp"

The result is:

enter image description here enter image description here

Which I think is fine... Note that in strings.xml the string direction for ar language is wrong but Android is drawing it properly. I guess it is because of some Android Studio setting.

mromer
  • 1,867
  • 1
  • 13
  • 18
0

Replace android:singleLine="true" with
android:lines="1" android:maxLines="1"

also add android:ellipsize="end"

Mickey Tin
  • 3,408
  • 10
  • 42
  • 71