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.
            Asked
            
        
        
            Active
            
        
            Viewed 8,224 times
        
    2 Answers
9
            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
 
    
    
        PankajAndroid
        
- 2,689
- 3
- 27
- 41
- 
                    `OnCreate(null)` will cause exception. You need to call `recreate()`; – Ioane Sharvadze Sep 03 '15 at 14:02
3
            
            
        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.
 
    
    
        dipali
        
- 10,966
- 5
- 25
- 51
- 
                    1Hey thanks for sharing this but it doesn't do any thing even with restarting the app – Antwan Sep 30 '15 at 19:16
