Disable auto change layout direction while using android:supportsRTL="true"

Abolfazl picture Abolfazl · Apr 25, 2017 · Viewed 7k times · Source

My application must support both RTL and LTR languages in the future but for now just RTLs. When I use android:supportsRTL="true" with android:layoutDirection="end" in each layout, NavigationView and Toolbar and everything else are fine.

But the only problem is when user change system language to a LTR language everything goes wrong because of layout direction changing.

WHAT I NEED: I have to set supportsRTL:"true". I need to change layout direction programmatically when user choose the "application" language at startup and not according to OS language.

QUESTION: Is there any way to prevent auto changing of layout direction by changing the OS language (not by setting supportsRTL false)?

See image below:

supportsRTL="true", RTL vs LTR

If I set android:supportRTL="false" and just design layouts for RTL, Toolbar and Navigation's menu direction will be my new troubles.

See image below:

supportsRTL="false"

Answer

Abolfazl picture Abolfazl · Apr 27, 2017

Solved. It was just a tiny horrible mistake.

But for anyone who has some problem like that I had this going to work very vell on android version 17 or above, for pre 17 I think the only way is to use separate layouts for RTL and LTR.

If you have to use android:supportsRTL="true" in your manifest then:
1. If you want to support just RTL language, to prevent from changing the direction according to OS language: Simply just add android:layoutDirection="rtl" to parent of all your layouts (Also you can use theme in styles.xml like this). Or make an sub activity and extends all your activity from it, then put this code in it's onCreate:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        Configuration configuration = getResources().getConfiguration();
            configuration.setLayoutDirection(new Locale("fa"));
        getResources().updateConfiguration(configuration, getResources().getDisplayMetrics());
    }

But in my mind it's better to set the direction to rtl in each layout because you can see how it's going to look without you run the app.

2. If you want to support both RTL and LTR languages, of course don't use android:layoutDirection="rtl/ltr" in your layouts Because it's direction won't change any way even by code. Because it's going to fix it in the direction rtl or ltr. so you should use the second one. make an sub activity and extends all your activity from it, then put this code in it (It's just for example):

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        Configuration configuration = getResources().getConfiguration();
        if (isSlectedLanguageRTL) {
            configuration.setLayoutDirection(new Locale("fa"));
        } else {
            configuration.setLayoutDirection(Locale.ENGLISH);
        }
        getResources().updateConfiguration(configuration, getResources().getDisplayMetrics());
    }

And at the end, sorry for my English :)