In my app I have a special menu where I can change application language.I get labels from project API(by parsing JSON) and project values xml.Can I change android application language without restart of application and сhangibg system language.
Insert this method and call it for changing the language.
private void setLocale (String localeCode , Bundle b ){
Log.d(TAG+"set location function: "+localeCode);
locale = new Locale(localeCode);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
getApplicationContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
UserDetail.this.getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
onCreate(null);
}
On toggle change or any selection call value like this:
setLocale("en-us",savedInstanceStat); // for english
setLocale("ar",savedInstanceStat); // for arabic
you can use toggle button to change language and set your selected language programamtically in your app without close app.
1.you will check which language selected?
String prefsToogleStr = getSharePrefrenceLocale();
Log.d("tag", "CtrlDashBoard prefsToogleStr" + prefsToogleStr);
if (prefsToogleStr.equalsIgnoreCase("en")) {
toggleLocaleButton.setChecked(true);
CommonMethod.setLocale("en", viewDashBoard);
} else {
CommonMethod.setLocale("ur", viewDashBoard);
toggleLocaleButton.setChecked(false);
}
////////////////////////////////////////
public String getSharePrefrenceLocale() {
SharedPreferences prefs = viewDashBoard.getSharedPreferences(
viewDashBoard.getPackageName(), ViewDashBoard.MODE_PRIVATE);
return prefs.getString("locale", "en");
}
2.change language in toggle button check change listener:
// Locale Toogle
toggleLocaleButton
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (buttonView.isChecked()) {
setSharePrefrenceLocale("en");
CommonMethod.setLocale("en", viewDashBoard);
} else {
setSharePrefrenceLocale("ur");
CommonMethod.setLocale("ur", viewDashBoard);
}
dialog.dismiss();
}
});
}
/////////////////////////////////////
public void setSharePrefrenceLocale(String locale) {
SharedPreferences prefs = viewDashBoard.getSharedPreferences(
viewDashBoard.getPackageName(), ViewDashBoard.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putString("locale", locale);
editor.commit();
}
//////////////////////////////////////
Main Method:call
public static void setLocale(String localeName, Context context) {
Locale locale = new Locale(localeName);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
context.getResources().updateConfiguration(config,
context.getResources().getDisplayMetrics());
}
i hope you understand .this is useful to you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With