How to get text direction in Android and change layout dynamically according to the direction?

℡╲_俬逩灬. 提交于 2019-11-27 20:39:56

Solution, was easier that I was thinking :) Thanks to JAVA

There is Bidi class. This class has getBaseLevel() method which returns 0 if your text is left-to-right otherwise 1 (if right-to-left).

So, this is my code:

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        Bidi bidi = new Bidi(userList.get(position).getName(), Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
        if(bidi.getBaseLevel() == 0)
            convertView = myInflater.inflate(R.layout.list_add_friends_row, null);
        else
            convertView = myInflater.inflate(R.layout.list_add_friends_row_mirror, null);

...

and result is:

=============

Update:

There is another method, baseIsLeftToRight() that might be better to be used in if statement. Result was same as above.

Bidi bidi = new Bidi(userList.get(position).getName(), Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
if(bidi.baseIsLeftToRight())
                convertView = myInflater.inflate(R.layout.list_add_friends_row, null);
            else
                convertView = myInflater.inflate(R.layout.list_add_friends_row_mirror, null);
Y.S

If the text is in a TextView, then all you have to do is

android:textDirection="anyRtl"

to force RTL. See the other options for your requirements.

There may be cases when mixed-language text is not to be shown in 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:

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.

Further references:

1. Write text file mix between arabic and english.

2. Unicode Bidirectional Algorithm.

Use textAlignment with gravity to get all texts RIGHT or LEFT

android:gravity="right"
android:textAlignment="gravity"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!