Monday, March 12, 2018

Set Locale from App - Back Button Issue

Leave a Comment

I am working on Language setting on my app. I was able to change the locale from my Main Activity through the use of

Resources resources = getResources(); Configuration configuration = resources.getConfiguration(); DisplayMetrics displayMetrics = resources.getDisplayMetrics(); configuration.setLocale(new Locale("ar")); resources.updateConfiguration(configuration,displayMetrics); recreate(); 

Everything worked fine but I noticed that the back button did not change its direction to RTL:

enter image description here

This is my expected behaviour when I set the language to a RTL language:

enter image description here

Is this possible?

3 Answers

Answers 1

Try to call this method when you set arabic language:

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) private void forceRTLIfSupported() {     if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){        //HERE CHECK CONDITION FOR YOUR LANGUAGE if it is AR then       //change if it is english then don't        getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);       } } 

Answers 2

Changing the navigation bar's direction means that the app will change the Locale of the device. Such a thing is similar to setting the device language in the language setting of the device since a Locale defines the language and direction (rtl or ltr) of the views to support the language. Since you're only changing the Locale of your Context in the app, it will not affect the Context of the other apps on the device and the system apps/components (launcher, navigation bar, status bar, etc...). Hence the navigation bar will not change and stay unaffected by the change in your app.

Furthermore, to alter the device Locale or any other configuration (font, font size, display metrics, rotation, etc..), the app would need the SET_CONFIGURATION permission to do so. Although it is not stated in the docs, the SET_CONFIGURATION permission can only be given automatically to system apps like the language and display settings. I haven't seen if it can be requested from the user by a third party app but I believe that it cannot be as most of the times in the development of my apps, the permission was being granted using adb shell pm grant command in the command line. Usually I would do that during my tests since my apps also support other languages and would want to make sure that the app performs well in such cases, hence I'd grant it the permission in the command line and let it to change the locale of the device from within the tests.

Answers 3

The direction of the navigation bar (and other parts of the OS, like the Settings), is not related to an app. A user can have a LTR OS with a RTL app. thus changing your app's locale and direction does not affect the underlying OS and the user has to change it manually.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment