3

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 device is set up. The emulator works, but when I install the apk on Samsung J5, which does work on Android 6.0.1 change language dont work. Is there any solution to my problem? Thank you.

 private void setLocale(String lang) {
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
    Locale locale = new Locale(lang);
    Locale.setDefault(locale);
    Configuration config = 
    getBaseContext().getResources().getConfiguration();
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());

} else {
    Resources resources = getBaseContext().getResources();
    Configuration configuration = resources.getConfiguration();
    configuration.setLocale(new Locale(lang));
    getBaseContext().getApplicationContext().createConfigurationContext(configuration);
}

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

}
janDe
  • 43
  • 1
  • 7

5 Answers5

13

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

@Override
public void applyOverrideConfiguration(Configuration overrideConfiguration) {
    if (overrideConfiguration != null) {
        int uiMode = overrideConfiguration.uiMode;
        overrideConfiguration.setTo(getBaseContext().getResources().getConfiguration());
        overrideConfiguration.uiMode = uiMode;
    }
    super.applyOverrideConfiguration(overrideConfiguration);
}
Deepak Goyal
  • 4,747
  • 2
  • 21
  • 46
3

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

NewestStackOverflowUser
  • 2,792
  • 5
  • 22
  • 31
1

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

EDIT

private void setLocale(String lang) {
    Locale locale = new Locale(lang);
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    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.Editor editor = getSharedPreferences("Settings", MODE_PRIVATE).edit();
    editor.putString("My_Lang", lang);
    editor.apply();
}
janDe
  • 43
  • 1
  • 7
1

UPDATE Jan 2022

New Way:

Set locale using the AndroidX library that will handle the storage itself for uper version of 7.0.0 (API level >24) - PerAppLanguages

March 2021

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

Old 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

 @Override
    protected void onCreate(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);
        ......
        .....
}

@Override
    protected void attachBaseContext(Context newBase) {
        String localeToSwitchTo= ContextUtils.getSavedLanguage(newBase);
        ContextWrapper localeUpdatedContext = ContextUtils.updateLocale(newBase, localeToSwitchTo);
        super.attachBaseContext(localeUpdatedContext);
    }

ContextUtils

public class ContextUtils  extends ContextWrapper {

    public ContextUtils(Context base) {
        super(base);
    }

    public static ContextWrapper updateLocale(Context context, String lang) {

        Resources resources = context.getResources();
        Configuration configuration = resources.getConfiguration();

        Locale localeToSwitchTo = new Locale(lang);

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

            configuration.setLocale(localeToSwitchTo);

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

        }else {

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

        return new ContextUtils(context);
    }

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

    public static void setSavedLanguage(Context context, String lang){

        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(
                context.getApplicationContext());

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

    }

}
Milan Maji
  • 363
  • 1
  • 5
  • 9