Skip to content Skip to sidebar Skip to footer

Changing Locale Programmatically Not Working On Android 6.0.1

I have a problem to change the language to Android 6.0.1, on new Android versions change language works well, but on 6.0.1 set up default string regardless of the language the devi

Solution 1:

This was not working for me. I tried all the solutions but not working on Android 6.0.1

I'm using the appCompat androidx.appcompat:appcompat:1.1.0

Please read this https://stackoverflow.com/a/58004553/3452078

I used the below code and it worked perfectly on all versions

@OverridepublicvoidapplyOverrideConfiguration(Configuration overrideConfiguration) {
    if (overrideConfiguration != null) {
        intuiMode= overrideConfiguration.uiMode;
        overrideConfiguration.setTo(getBaseContext().getResources().getConfiguration());
        overrideConfiguration.uiMode = uiMode;
    }
    super.applyOverrideConfiguration(overrideConfiguration);
}

Solution 2:

If you are distributing your app using aap make sure to guarantee that all language strings are bundled inside the apk that the user downloads from Google Play.

You can update your app build.gradle by adding the following:

android {
  bundle {
     language {
     // Specifies that the app bundle should not support// configuration APKs for language resources. These// resources are instead packaged with each base and// dynamic feature APK.
     enableSplit = false
     }
  }
}

This answer is based on https://stackoverflow.com/a/54862243/3296947

Solution 3:

Solution 4:

To close the topic because I managed to edit my code, maybe somebody needs it.

EDIT

privatevoidsetLocale(String lang) {
    Localelocale=newLocale(lang);
    Locale.setDefault(locale);
    Configurationconfig=newConfiguration();
    if (Build.VERSION.SDK_INT >= 24) {
        config.setLocale(locale);
        getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
    } else {
        config.locale = locale;
        getBaseContext().getApplicationContext().createConfigurationContext(config);
    }

    // shared pref.
    SharedPreferences.Editoreditor= getSharedPreferences("Settings", MODE_PRIVATE).edit();
    editor.putString("My_Lang", lang);
    editor.apply();
}

Solution 5:

March 2021

I used the below code and it worked perfectly on all versions.

Full code:

Note: For 6.0.1 version must update locale before setContentView(R.layout.activity_main) and for uper version of 7.0.0 (API level >24) need to override protected void attachBaseContext(Context newBase) in all Activity of your project.

MainActivity

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.N) {
            ContextUtils.updateLocale(MainActivity.this, ContextUtils.getSavedLanguage(MainActivity.this));
        }

        setContentView(R.layout.activity_main);
        ......
        .....
}

@OverrideprotectedvoidattachBaseContext(Context newBase) {
        String localeToSwitchTo= ContextUtils.getSavedLanguage(newBase);
        ContextWrapperlocaleUpdatedContext= ContextUtils.updateLocale(newBase, localeToSwitchTo);
        super.attachBaseContext(localeUpdatedContext);
    }

ContextUtils

publicclassContextUtilsextendsContextWrapper {

    publicContextUtils(Context base) {
        super(base);
    }

    publicstatic ContextWrapper updateLocale(Context context, String lang) {

        Resourcesresources= context.getResources();
        Configurationconfiguration= resources.getConfiguration();

        LocalelocaleToSwitchTo=newLocale(lang);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

            configuration.setLocale(localeToSwitchTo);

            LocaleListlocaleList=newLocaleList(localeToSwitchTo);
            LocaleList.setDefault(localeList);
            configuration.setLocales(localeList);
            context = context.createConfigurationContext(configuration);

        }else {

            Locale.setDefault(localeToSwitchTo);
            configuration.locale=localeToSwitchTo;
            resources.updateConfiguration(configuration, resources.getDisplayMetrics());
        }

        returnnewContextUtils(context);
    }

    publicstatic String getSavedLanguage(Context context) {
        SharedPreferencespreferences= PreferenceManager.getDefaultSharedPreferences(
                context.getApplicationContext());
        return preferences.getString("SETTINGS_LANG", "en");
    }

    publicstaticvoidsetSavedLanguage(Context context, String lang){

        SharedPreferencespreferences= PreferenceManager.getDefaultSharedPreferences(
                context.getApplicationContext());

        SharedPreferences.Editoreditor= preferences.edit();
        editor.putString("SETTINGS_LANG", lang);
        editor.apply();

    }

}

Post a Comment for "Changing Locale Programmatically Not Working On Android 6.0.1"