Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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"

like image 392
Abolfazl Avatar asked Apr 25 '17 19:04

Abolfazl


People also ask

How do I disable RTL on android?

How do I disable RTL support on android? Change all of your app's "left/right" layout properties to new "start/end" equivalents. If you are targeting your app to Android 4.2 (the app's targetSdkVersion or minSdkVersion is 17 or higher), then you should use “start” and “end” instead of “left” and “right”.

What is layout direction in android?

A layout direction can be left-to-right (LTR) or right-to-left (RTL). It can also be inherited (from a parent) or deduced from the default language script of a locale.

How to enable RTL support in android?

First of all, you must add android:supportsRtl="true" to the <application> element in your manifest file for your app to supporting RTL design. Trick: If your app supports multiple languages and if you have code snippet like config. setLayoutDirection(Locale.US) you must change it.

How do I change RTL on android?

Just go to Android Studio > Refactor > Add RTL support where possible… I would recommend you checking your app once after applying this change as you might not want all your Layouts/Views to be RTL. If you want to force any layout to LTR then just add android:layoutDirection="ltr" to that view.


1 Answers

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

like image 68
Abolfazl Avatar answered Sep 30 '22 17:09

Abolfazl