Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't android recognize the locale as RTL when set programmatically?

I have written a multi-lingual app.

I have different values folder for different languages like values-fa and values-ru. As some of these languages like Farsi and Arabic are RTL, I used a different layout design by putting some other layout .xml files in a folder layout-ldrtl beside the main layout folder.

I also put a file named is_right_to_left.xml int a folder named values-ldrtl which defines a variable like this:

<bool name="is_right_to_left">true</bool>

I have the same file in values folder and set the variable value to false there.

Whenever in the code I need to know, I use this line to understand if current locale is a RTL one or not:

isRightToLeft = context.getResources().getBoolean(R.bool.is_right_to_left);

I've tested the app with different device configs and everything works like charm. For example setting device language to Farsi causes the app to read the strings from values-fa and the layouts from layout-ldrtl, while when Arabic is set for device, values are read from values-ar and the layouts are also read from layout-ldrtl because both of them are RTL languages. It of-course works correctly for LTR languages too. That's what I want.

The problem occurs when I set the locale of the app programmatically. This is the code I wrote in onCreate() method of my MainActivity:

Locale myLocale = new Locale("fa");
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);

Now, the values are read correctly according to the language, but layout-ldrtl and values-ldrtl are never read. So, the app would load in LTR design because of wrong .xml files and wrong value of isRightToLeft variable. One solution I've tested is putting those .xml files in each language layout folder separately, and it works. But I hope to get a better and neater way of solving the problem. And besides the solution, why does android act in this way?

like image 925
EmJiHash Avatar asked May 10 '16 14:05

EmJiHash


People also ask

How to set RTL in android?

In your android phone, tap on “Settings” icon. Now tap on developer options and search for “Force RTL layout direction”. Tap on it to enable RTL feature.

What is RTL 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 localization works in android?

In order to localize the strings used in your application , make a new folder under res with name of values-local where local would be the replaced with the region. Once that folder is made, copy the strings. xmlfrom default folder to the folder you have created. And change its contents.

How to enable RTL design 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. Otherwise, your app will not support the RTL design.

How do I set locale programmatically in Android?

This example demonstrates how do I set locale programmatically in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml.

Why is setlocale(locale) not working in Android 6 and 7?

If you use this, don't forget to save the language to SharedPreferences when you set the locale with setLocale (locale) You might be experiencing issues in Android 6 and 7, and this happens due to an issue in the androidx libraries while handling the night mode.

How do I Check my App in RTL mode?

You can check your app in RTL mode by doing one of two things. Select one of the RTL languages as your device language. Or from Developer Options in Settings, check Force RTL layout Direction. I find this option more convenient but, do note, this does not change your System language.


1 Answers

try this:

Resources res = getResources();
 Configuration newConfig = new Configuration( res.getConfiguration() );
 Locale locale = new Locale("fa");
 Locale.setDefault(locale);
 newConfig.locale = locale;
 newConfig.setLayoutDirection( locale );
 res.updateConfiguration( newConfig, null );
like image 159
gilgil28 Avatar answered Sep 25 '22 16:09

gilgil28