javaandroidlocale

Why does changing the in app language work on some devices but not others


I can change the language locale on my Galaxy S23 Ultra, but when I try on my Galaxy A14 5g, the language remains the same. They are both running Android 14. I don't understand what I am doing wrong. Here is the code I am using:

String languageToLoad = "es";
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;

MainActivity.this.getResources().updateConfiguration(config, MainActivity.this.getResources().getDisplayMetrics());

Intent intent = new Intent(MainActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

I've tried changing the xml string file name. It is currently values-es. I've tried changing the bundle language enableSplit to false. These did not work what can I do differently?


Solution

  • From SDK 33 (Tiramisu), instead of using updateConfiguration the recommended way to change language is to use setApplicationLocales():

            LocaleListCompat aLocale = LocaleListCompat.forLanguageTags(locale.getLanguage());
            AppCompatDelegate.setApplicationLocales(aLocale);
    

    It is worth to note that: This API should always be called after Activity.onCreate(), apart from any exceptions explicitly mentioned in this documentation.

    Reference: setApplicationLocales