I am trying to change the app language programmatically when language name is clicked on in the navigation drawer list. The language changes but I am not able to maintain when the app is closed.
here my setLocal
method
private void setLocale(String lang) {
Locale locale = new Locale(lang);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());
SharedPreferences.Editor editor = getSharedPreferences(SHARED_PREFS,MODE_PRIVATE).edit();
editor.putString(MY_LANG,lang);
editor.apply();
}
I call this method when I want to change the lang like this
if (id == R.id.ar_lang){
setLocale("ar");
recreate();
}
if (id == R.id.eng_lang){
setLocale("en");
recreate();
}
and I call this method in onCreate to get the stored language, but it doesn't work.
public void loadLocale(){
SharedPreferences prefs = getSharedPreferences(SHARED_PREFS, Activity.MODE_PRIVATE);
String languages = prefs.getString(MY_LANG,"");
setLocale(languages);
}