How to change Android O / Oreo / api 26 app language How to change Android O / Oreo / api 26 app language android android

How to change Android O / Oreo / api 26 app language


I had the same problem: since Android 8.0+ some parts of my app did't change their language anymore. Updating of both application and activity context helps me. Here is an example of MainActivity function:

private void setApplicationLanguage(String newLanguage) {    Resources activityRes = getResources();    Configuration activityConf = activityRes.getConfiguration();    Locale newLocale = new Locale(newLanguage);    activityConf.setLocale(newLocale);    activityRes.updateConfiguration(activityConf, activityRes.getDisplayMetrics());    Resources applicationRes = getApplicationContext().getResources();    Configuration applicationConf = applicationRes.getConfiguration();    applicationConf.setLocale(newLocale);    applicationRes.updateConfiguration(applicationConf,     applicationRes.getDisplayMetrics());}


Yes in android Oreo localization is not working fine with updateconfiguration. But it is deprecated in android N itself. Instead of updateconfiguration use createconfiguration in each attachcontext. it is working fine for me. Try this...

In you activity add this..

@Overrideprotected void attachBaseContext(Context newBase) {    if(Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1) {        super.attachBaseContext(MyContextWrapper.wrap(newBase, "ta"));    }    else {        super.attachBaseContext(newBase);    }}

In MyContextWrapper.java

 public static ContextWrapper wrap(Context context, String language) {    Resources res = context.getResources();    Configuration configuration = res.getConfiguration();    Locale newLocale = new Locale(language);    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {        configuration.setLocale(newLocale);        LocaleList localeList = new LocaleList(newLocale);        LocaleList.setDefault(localeList);        configuration.setLocales(localeList);        context = context.createConfigurationContext(configuration);    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {        configuration.setLocale(newLocale);        context = context.createConfigurationContext(configuration);    } else {        configuration.locale = newLocale;        res.updateConfiguration(configuration, res.getDisplayMetrics());    }    return new ContextWrapper(context);}


updateConfiguration is deprecated and you should use createConfigurationContext. I solved it this way:

    @Override    protected void attachBaseContext(Context newBase) {        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {            Configuration config = newBase.getResources().getConfiguration();            //Update your config with the Locale i. e. saved in SharedPreferences            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(newBase);            String language = prefs.getString(SP_KEY_LANGUAGE, "en_US");            Locale.setDefault(locale);            config.setLocale(new Locale(language));            newBase = newBase.createConfigurationContext(config);        }        super.attachBaseContext(newBase);    }