5

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?

Aleksandar G
  • 1,163
  • 2
  • 20
  • 25
EmJiHash
  • 623
  • 9
  • 22
  • Do you need to do this programmatically, or are you just testing your app? If that latter, check out http://lifehacker.com/enable-androids-secret-right-to-left-layout-if-youre-le-1676267178 – petey May 10 '16 at 14:20
  • @petey I need to do it programmatically. One case is letting the user to choose the app language. Thanks for answer though. – EmJiHash May 10 '16 at 14:23

1 Answers1

5

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 );
gilgil28
  • 540
  • 5
  • 12
  • Thanks, It's a good answer and it works, but only for API 17+. Actually, I did the whole two-layout-folder way for minus 17 API support. – EmJiHash May 10 '16 at 14:49
  • I up-voted the answer, but didn't mark it as accepted. Waiting for a solution that may work for previous APIs as well. – EmJiHash May 11 '16 at 05:56