1

Changing locale programmatically like below,

Locale locale = new Locale("es");
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getBaseContext().getResources().updateConfiguration(config,
                getBaseContext().getResources().getDisplayMetrics());

        Resources.getSystem().updateConfiguration(config, null);

Is not working for nougat devices, It automatically changes to english when we switch from one activity to another

Vennila
  • 240
  • 1
  • 11
  • In other devices is it working well while switching from one activity to another ? – Piyush Nov 04 '16 at 12:06
  • yes.. It is working fine.. only the nougat devices I faced this issue – Vennila Nov 04 '16 at 12:06
  • I had similar issue when launching webview. Here is the question with complete solution http://stackoverflow.com/questions/40486932/android-nougat-7-1-resets-locale-after-launching-webview – Maher Abuthraa Mar 10 '17 at 09:46

1 Answers1

2

Nougat has deprecated the config.locale. Use setLocales() instead.

Configuration config = activity.getBaseContext().getResources().getConfiguration();

Locale locale = Utils.stringToLocale(stringLanguage);
Locale.setDefault(locale);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
      config.setLocales(new LocaleList(locale));
} else {
      config.locale = locale;
}
activity.getBaseContext().getResources().updateConfiguration(config,
            activity.getBaseContext().getResources().getDisplayMetrics());
Sadeshkumar Periyasamy
  • 4,848
  • 1
  • 26
  • 31