2

I've got the following issue - Locale can't be changed on my device running 4.4.2 (Gigabyte Mika M3), but it is perfectly working in android emulator running API 26.

I'm changing locale in the following way:

Resources resources = getResources();
        Configuration configuration = resources.getConfiguration();
        DisplayMetrics displayMetrics = resources.getDisplayMetrics();
        configuration.setLocale(locale);

        resources.updateConfiguration(configuration,displayMetrics);
        getApplicationContext().createConfigurationContext(configuration);

I also tried that way - but it's not working too:

 Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getBaseContext().getResources().updateConfiguration(config,
                getBaseContext().getResources().getDisplayMetrics());

I store location as string in shared preferences like this:

SharedPreferences sharedPreferences = getSharedPreferences("languages", MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();

    editor.putString("language", locale.getLanguage());
    editor.apply();

and after all of this i'm recreating activity

Intent refresh = new Intent(ChooseLanguageActivity.this, ChooseLanguageActivity.class);
    startActivity(refresh);
    finish();

The problem is - recreated activity (this) opens with the same language as earlier and application on restart opens with default locale :C

Nikita Yankov
  • 145
  • 1
  • 9

1 Answers1

0

Your implementaion modifies the locale of global applicationContext.

on my android-4.4.2 cellphone (wiko lenny) changing locale works without any problem using this https://stackoverflow.com/a/34675427/519334 implementation.

here the trick is that you have to change the locale of every-s activity in onCreate before calling the super method:

@Override
protected void onCreate(Bundle savedInstanceState) {
    fixLocale(this);
    super.onCreate(savedInstanceState);
}

Interisting to see that higher android versions allow to modify the locale of global applicationContext

k3b
  • 14,517
  • 7
  • 53
  • 85
  • Thanks for the response. I tried your way and unfortunately its not working for me too. Either its real device or emulator. – Nikita Yankov Aug 17 '17 at 06:30