36

I use this code to set the default language manually in multiple languages app:

public static void setLanguage(Context context, String languageCode){
    Locale locale = new Locale(languageCode);
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;  // Deprecated !!
    context.getApplicationContext().getResources().updateConfiguration(config,
            context.getResources().getDisplayMetrics());
}

so now we cant set locale by config.locale is deprecated because the variable is going to be removed in API 24.

So i saw that the alternative is to set:

config.setLocales();

locale

Added in API level 1

Locale locale

This field was deprecated in API level 24. Do not set or read this directly. Use getLocales() and setLocales(LocaleList). If only the primary locale is needed, getLocales().get(0) is now the preferred accessor.

Current user preference for the locale, corresponding to locale resource qualifier.

I noticed also that there is setLocale(Locale) as well but for api 17 and above

I checked the setLocales(LocalList) documentation and it is marked in grey as if it is also deprecated !

so what can be solution for this !?

MBH
  • 16,271
  • 19
  • 99
  • 149

1 Answers1

39

Hope this helps, I also added the getters.

@SuppressWarnings("deprecation")
public Locale getSystemLocaleLegacy(Configuration config){
    return config.locale;
}

@TargetApi(Build.VERSION_CODES.N)
public Locale getSystemLocale(Configuration config){
    return config.getLocales().get(0);
}

@SuppressWarnings("deprecation")
public void setSystemLocaleLegacy(Configuration config, Locale locale){
    config.locale = locale;
}

@TargetApi(Build.VERSION_CODES.N)
public void setSystemLocale(Configuration config, Locale locale){
    config.setLocale(locale);
}

public static void setLanguage(Context context, String languageCode){
    Locale locale = new Locale(languageCode);
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        setSystemLocale(config, locale);
    }else{
        setSystemLocaleLegacy(config, locale);
    }
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1)
        context.getApplicationContext().getResources().updateConfiguration(config,
            context.getResources().getDisplayMetrics());
}

UPDATE 12/3/2016

Since context.getApplicationContext().getResources().updateConfiguration() is now deprecated, I highly recommend that you check out this solution and adopt a different approach to overwriting Android system configuration.

Better Solution: https://stackoverflow.com/a/40704077/2199894

Community
  • 1
  • 1
Bassel Mourjan
  • 3,604
  • 3
  • 26
  • 37
  • 1
    for setSystemLocale(. version N .) google recommends to user setLocales instead of setLocale. you can easily add a list the locales even if you have only one: config.setLocales(new LocaleList( locale)); https://developer.android.com/reference/android/content/res/Configuration.html#setLocale(android.os.Locale) – Jose M Lechon Sep 20 '16 at 15:25
  • 1
    Deprecation is only for direct access to config.locale in order to force developers to use setters and getters. Also, setLocale is introduced in the last N release so it is safe to use it in case one doesn't have more than one locale. – Bassel Mourjan Sep 20 '16 at 15:49
  • 2
    Hey, I really liked ur answer, but you posted a link with a better solution, could you please briefly explain why is it better? if I'm not mistaken, the "better solution" suggests to run that piece of code in every activity, which I would prefer to avoid if possible, even though I have a BaseActivity... Thanks in advance ! – Vladislav Kan Feb 10 '17 at 04:24
  • For Samsung devices with 6.0.1, there is a issue. Locale is working for only 1st time.. from second time it is taking device default.. Any solution for this? – Pandiri Deepak Jun 06 '19 at 06:25