Reputation: 258
I'm trying to change my app language and everything is working well, but menu strings don't change after first time and it needs me to change language to the same language again to make menu strings change
I'm using sharedpreferenses to save the language which user wants to choose, and in activity i check first if the sharedpreferencse have saved language or not and i change language based on which language saved in sharedpreferences.
this's the code in the activity which i use to change app language
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
android.content.res.Configuration conf = res.getConfiguration();
conf.setLocale(new Locale(language.toLowerCase()));
res.updateConfiguration(conf, dm);
Upvotes: 3
Views: 487
Reputation: 258
I found solution, but i think it's dirty solution and thier are of course better solutions. i just hope it'll help anyone couldn't find solution like me.
i just changed fragment's code which change language to be like this.
public void saveData(String lang,String curr){
if (iLang == 0){
Toast.makeText(getActivity(), "Choose your language, please.", Toast.LENGTH_SHORT).show();
}else{
SharedPreferences mPrefs = getActivity().getSharedPreferences("fetch", MODE_PRIVATE);
SharedPreferences.Editor prefsEditor = mPrefs.edit();
prefsEditor.putString("lang", lang);
prefsEditor.putString("curr", curr);
prefsEditor.apply();
getActivity().recreate();
Intent intent = getActivity().getIntent();
getActivity().finish();
startActivity(intent);
}
}
and it was like this.
public void saveData(String lang,String curr){
if (iLang == 0){
Toast.makeText(getActivity(), "Choose your language, please.", Toast.LENGTH_SHORT).show();
}else{
SharedPreferences mPrefs = getActivity().getSharedPreferences("fetch", MODE_PRIVATE);
SharedPreferences.Editor prefsEditor = mPrefs.edit();
prefsEditor.putString("lang", lang);
prefsEditor.putString("curr", curr);
prefsEditor.apply();
Intent intent = new Intent(getActivity,HomeActivity.class);
startActivity(intent);
}
}
And i'll be thankful if someone can edit this solution for me to another better.
Upvotes: 1