Change the language of Android applications

In my application, I have a special menu in which I can change the language of the application. I get shortcuts from the project API (by parsing JSON) and the xml.Can project values. I change the language of the Android applications without restarting the application and the system language hangibg.

+6
source share
2 answers

Insert this method and call it to change 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); } 

Turn on switching or any call invocation value as follows:

 setLocale("en-us",savedInstanceStat); // for english setLocale("ar",savedInstanceStat); // for arabic 
+7
source

You can use the toggle button to change the language and programmatically set the selected language in the application without a closed application.

1. will you check which language is 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 the language in the switch for changing the listening switch button:

 // 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(); } 

//////////////////////////////////////

The 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 good for you.

+3
source

All Articles