How can you assign a locale
and get the locale
current on Android N?
Configuration conf = getResources().getConfiguration();
conf.locale = new Locale("es") // Deprecated
String language = conf.locale.getDisplayName() //Deprecated
How can you assign a locale
and get the locale
current on Android N?
Configuration conf = getResources().getConfiguration();
conf.locale = new Locale("es") // Deprecated
String language = conf.locale.getDisplayName() //Deprecated
In this SO I found the solution .
Locale locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
locale = context.getResources().getConfiguration().getLocales().get(0);
} else {
locale = context.getResources().getConfiguration().locale;
}
Starting with Android N, it should be done like this:
Configuration conf = getResources().getConfiguration();
conf.setLocale = new Locale("es") // VERSION_CODES.JELLY_BEAN_MR1+
String language = conf.getLocales().get(0).toString() // VERSION_CODES.N+
Another alternative to what you are putting would be the following:
To obtain the current locale:
Configuration config = context.getResources().getConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
locale = getSystemLocale(config);
} else {
locale = getSystemLocaleLegacy(config);
}
and to update the locale:
Locale locale = new Locale("es");
Locale.setDefault(locale);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
setSystemLocale(config, locale);
} else {
setSystemLocaleLegacy(config, locale);
}