Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android mixed language text - BidiFormatter on String with RTL and LTR text

I have a ListView with custom View where I have a TextView :

 <TextView
     android:id="@+id/textViewItemTitle"
     android:layout_width="0dp"
     android:layout_weight="1"
     android:layout_height="wrap_content"
     android:gravity="right|center_horizontal"
     android:text="title" />

This TextView contains Hebrew text.

 if(!bidi.isRtl(event)){
     event = bidi.unicodeWrap(event);
 }

 holder.title.setText(String.format("%s  %s  %s", bidi.unicodeWrap(item.getStartTimeNoDate().trim()), event,
 bidi.unicodeWrap(item.getDuration().trim())));

Where the first argument is time hh:mm:ss, second (event) is a Hebrew String and third like the first.

The problem: some time the event String contains mixed text in Hebrew and English like abc-אבג then all the text behave like the the gravity is left (and not right like I defined in the text view), I mean indented to left.

How to solve that?

like image 352
NickF Avatar asked Dec 09 '13 14:12

NickF


3 Answers

I had the same issue and I am targeting API 16 My solution was very simple, I added to the beginning of the String "\u200F"

String mixedLanguageText = ... // mixed-language text
newText = "\u200F" + mixedLanguageText;

"\u200F" = Unicode Character 'RIGHT-TO-LEFT MARK' (U+200F)

like image 80
tm81 Avatar answered Oct 19 '22 13:10

tm81


Try adding to your TextView:

android:textDirection="anyRtl"

For more reading: http://developer.android.com/reference/android/view/View.html#attr_android:textDirection

like image 40
idog Avatar answered Oct 19 '22 12:10

idog


The accepted answer will do the job when the text is in a TextView. This is a more general answer, applicable both to the basic/happy scenario and to other, more complicated use cases.

There are situations when mixed-language text is to be used someplace other than inside a TextView. For instance, the text may be passed in a share Intent to Gmail or WhatsApp and so on. In such cases, you must use a combination of the following classes:

  • BidiFormatter
  • TextDirectionHeuristics

As quoted in the documentation, these are ...

Utility class[es] for formatting text for display in a potentially opposite-directionality context without garbling. The directionality of the context is set at formatter creation and the directionality of the text can be either estimated or passed in when known.

So for example, say you have a String that has a combination of English & Arabic, and you need the text to be

  • right-to-left (RTL).
  • always right-aligned, even if the sentence begins with English.
  • English & Arabic words in the correct sequence and without garbling.

then you could achieve this using the unicodeWrap() method as follows:

String mixedLanguageText = ... // mixed-language text

if(BidiFormatter.getInstance().isRtlContext()){
    Locale rtlLocale = ... // RTL locale
    mixedLanguageText = BidiFormatter.getInstance(rtlLocale).unicodeWrap(mixedLanguageText, TextDirectionHeuristics.ANYRTL_LTR);
}

This would convert the string into RTL and align it to the left, if even one RTL-language character was in the string, and fallback to LTR otherwise. If you want the string to be RTL even if it is completely in, say English (an LTR language), then you could use TextDirectionHeuristics.RTL instead of TextDirectionHeuristics.ANYRTL_LTR.

This is the proper way of handling mixed-direction text in the absence of a TextView. Interestingly, as the documentation states,

Also notice that these direction heuristics correspond to the same types of constants provided in the View class for setTextDirection(), such as TEXT_DIRECTION_RTL.

Update:

I just found the Bidi class in Java which seems to do something similar. Look it up!

Further references:

1. Write text file mix between arabic and english.

2. Unicode Bidirectional Algorithm.

like image 34
Y.S Avatar answered Oct 19 '22 13:10

Y.S