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

3

10

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"

Biron answered 25/4, 2017 at 19:19 Comment(0)
B
19

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 :)

Biron answered 27/4, 2017 at 4:1 Comment(2)
Are you sure this completely worked? I want use this for Persian language like you, is this okay for that?Aggregate
This Solution is Working for me, Thank you.Protactinium
C
2

In your manifest file and inside the application tag you can add these two lines.

<manifest>
    <application
        .
        .
        .
        android:supportsRtl="false"
        tools:replace="android:supportsRtl" //(replace libraries' Rtl support with ours)
        >
    </application>
</manifest>

Some libraries have support Rtl in their manifest file so if you want to use those libraries you must replace their manifest line of code with yours.

this problem can be solved :)

Coeternity answered 6/5, 2018 at 6:41 Comment(0)
E
0

Just add something like this in your App Theme Style:

<item name="android:layoutDirection">ltr</item>

This is worked for me.

Electrokinetics answered 31/5, 2021 at 5:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.