Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change language for a whole android application?

I want to write a application that can display either english or chinese. I have already prepared 2 string.xml which is value/strings.xml and value-zh-rHK/strings.xml. But I have no idea how to change the language via ListPreference of android.

xml/preferences.xml:

<?xml version="1.0" encoding="utf-8"?>

<SwitchPreference
    android:key="pref_nightmode"
    android:title="@string/nightmode"
    android:defaultValue="false">
</SwitchPreference>

<ListPreference
    android:key="pref_lang"
    android:title="@string/lang"
    android:dialogTitle="Choose Language"
    android:entries="@array/lang"
    android:entryValues="@array/lang_value"
    android:defaultValue="@string/lang_default">
</ListPreference>

and the Preferences.java

public class Preferences extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitle(drawer_menu[5]);
    getFragmentManager().beginTransaction().replace(R.id.content_frame, new MainPreferenceFragment()).commit();
}

public static class MainPreferenceFragment extends PreferenceFragment {
    String locale;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
        PreferenceManager pm = getPreferenceManager();
        ListPreference lang = (ListPreference) pm.findPreference("pref_lang");
        if(lang.getValue().equals("English")) {
            locale = "en_US";
        } else {
            locale = "zh_HK";
        }
    }
}

The activity extends BaseActivity because I have a drawer menu right there.

like image 926
rascee Avatar asked Apr 28 '16 17:04

rascee


People also ask

How do I change the project language in Android Studio?

Go to app > res > values > right-click > New > Value Resource File and name it as strings. Now, we have to choose qualifiers as Locale from the available list and select the language as Hindi from the drop-down list. Below is the picture of the steps to be performed.


1 Answers

You can change language settings for app only. Use Locale class and update default configuration. Language change only applied when activity restart or re-start application. Use following locale class

public class LocaleUtils {

    private static Locale sLocale;

    public static void setLocale(Locale locale) {
        sLocale = locale;
        if(sLocale != null) {
            Locale.setDefault(sLocale);
        }
    }

    public static void updateConfig(ContextThemeWrapper wrapper) {
        if(sLocale != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            Configuration configuration = new Configuration();
            configuration.setLocale(sLocale);
            wrapper.applyOverrideConfiguration(configuration);
        }
    }

    public static void updateConfig(Application app, Configuration configuration) {
        if(sLocale != null && Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
            //Wrapping the configuration to avoid Activity endless loop
            Configuration config = new Configuration(configuration);
            config.locale = sLocale;
            Resources res = app.getBaseContext().getResources();
            res.updateConfiguration(config, res.getDisplayMetrics());
        }
    }
}

Application class :

public class App extends Application {
    public void onCreate(){
        super.onCreate();
        // get user preferred language set locale accordingly new locale(language,country)
        LocaleUtils.setLocale(new Locale("iw"));
        LocaleUtils.updateConfig(this, getBaseContext().getResources().getConfiguration());
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        LocaleUtils.updateConfig(this, newConfig);
    }
}

BaseActivity :

public class BaseActivity extends Activity {
    public BaseActivity() {
        LocaleUtils.updateConfig(this);
    }
}

Above answer is taken from : Changing Locale within the app itself

Android Document : http://developer.android.com/reference/java/util/Locale.html

like image 175
USKMobility Avatar answered Nov 11 '22 13:11

USKMobility