Define and obtain the current Locale of Configuration in Android N?

3

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
    
asked by Webserveis 16.09.2016 в 13:14
source

2 answers

3

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+
    
answered by 16.09.2016 / 13:23
source
1

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);
}
    
answered by 25.07.2017 в 10:33