Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing android application language [duplicate]

Tags:

android

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.

like image 398
Artem Cherkasov Avatar asked Jan 13 '14 07:01

Artem Cherkasov


2 Answers

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
like image 183
PankajAndroid Avatar answered Sep 27 '22 19:09

PankajAndroid


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.

like image 30
dipali Avatar answered Sep 27 '22 21:09

dipali