Android: Change language programmatically

27,868

Solution 1

So, because of only two activities (two menu modes) where i let to change locale, I just restart Activity right after locale change, so everything is refreshed. Didn't find any other better solution

Solution 2

Resources res = context.getResources();
// Change locale settings in the app.

DisplayMetrics dm = res.getDisplayMetrics();

android.content.res.Configuration conf = res.getConfiguration();

conf.locale = new Locale(language_code.toLowerCase());
res.updateConfiguration(conf, dm);

If you have language specific content - you can change that base on the setting.## Heading ##

Solution 3

I would not recommend to change the language within your application. A better solution would be a button that opens the systems locale settings. You can use the following intent for this.

startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCALE, 0);

For more info about the locale settings: http://developer.android.com/reference/android/provider/Settings.html#ACTION_LOCALE_SETTINGS

Share:
27,868
Paulius Vindzigelskis
Author by

Paulius Vindzigelskis

Updated on September 11, 2020

Comments

  • Paulius Vindzigelskis
    Paulius Vindzigelskis almost 4 years

    I have 3 buttons to change language. So I need to make method, where I could set listeners to buttons which could change language everytime they are pressed. I tried this https://stackoverflow.com/a/2900144/1088229 but this way it changes locale only once and seems that this doesn't work any more (if i click again, it doesn't even respond). So I added changeLanguageListener() in end of listener, so listener is refreshed.

    So is it ok, how i solved it or there is another way?

    private void changeLanguageListener() {
        final Button butEn = (Button) findViewById(R.id.button_language_en);
        final Button butLt = (Button) findViewById(R.id.button_language_lt);
        final Button butRu = (Button) findViewById(R.id.button_language_ru);
        OnClickListener listener = new OnClickListener() {
    
            public void onClick(View v) {
                Button but = (Button) v;
                Resources res = getResources();
                String current = res.getConfiguration().locale.getCountry();
                Log.i("Current", current);
                String localeString = new String(current);
                if (but.equals(butEn)) {
                    localeString = "en";
                } else if (but.equals(butLt)) {
                    localeString = "lt";
                } else if (but.equals(butRu)) {
                    localeString = "ru";
                }
                Log.i("Clicked", localeString);
    
                if (!current.equalsIgnoreCase(localeString) && localeString.length() > 0) {
                    // Change locale settings in the app.
                    DisplayMetrics dm = res.getDisplayMetrics();
                    android.content.res.Configuration conf = res.getConfiguration();
                    conf.locale = new Locale(localeString.toLowerCase());
                    res.updateConfiguration(conf, dm);  
                    //refresh menu
                    setGridView();
                    //added this line to get refreshed listener
                    changeLanguageListener();
                }               
            }
        };
        butEn.setOnClickListener(listener);
        butLt.setOnClickListener(listener);
        butRu.setOnClickListener(listener);
    }