Reputation: 250
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
Upvotes: 1
Views: 1433
Reputation: 4908
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());
Upvotes: 2