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

前端 未结 8 1498
深忆病人
深忆病人 2020-12-01 04:37

I want to change the language of the app and this works fine until API 26.

For api > 25 I put Locale.setDefault(Locale.Category.DISPLAY, mynewlanglocale);

8条回答
  •  囚心锁ツ
    2020-12-01 05:15

    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);
        }
    

提交回复
热议问题