1

I'm using this below method to change the language of the app by passing the language code as string.
When I change the screen orientation the language rests to default language and all views resets too.

 public String setLocale(String lang) {
    Locale myLocale = new Locale(lang);
    Resources res = getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    conf.locale = myLocale;
    res.updateConfiguration(conf, dm);
    Intent refresh = new Intent(this, Login.class);
    startActivity(refresh);
    finish();
    return lang;
}

I tried to use onSaveInstanceState and onRestoreInstanceState, but I don't know how to make all these methods work together.

  • 3
    save your current language in permanent storage and implement onOrientationChanged method in your code and in it check for language and update accordingly – Vivek Mishra Jan 04 '16 at 12:10
  • 1
    Store current language in SharedPreferences and when screen orientation changed set language from SharedPreferences Also check out : http://stackoverflow.com/questions/2324418/android-forced-locale-resetted-on-orientation-changes – Haresh Chhelana Jan 04 '16 at 12:13

1 Answers1

0

Using shared preferences you can store the lanuage and get the stored value on screen orientation change.

public String setLocale(String lang) {
    Locale myLocale = new Locale(lang);
    Resources res = getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    conf.locale = myLocale;
    res.updateConfiguration(conf, dm);
    Intent refresh = new Intent(this, Login.class);
    startActivity(refresh);
    finish();
    // save shared preference here or later, your choice.
    return lang;
}

Code to create and save shared preference.

SharedPreferences  preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor nameEditor = preferences.edit();
nameEditor.putString("saved_lang", lang);
nameEditor.commit();

Code to retrieve value of shared preference.

//To get language when screen changes.
String lang = preferences.getString("saved_lang", "");

You can use the same principal to save other values and settings.