Skip to content Skip to sidebar Skip to footer

How To Make Stay In The Last Language When Android App Is Closed?

I have already made an app android to change the language. But when the app is closed and the open again, the language back to the default language. How can i make when app open ag

Solution 1:

you need to save the user selected language to shared preference or any other persistent storage, on next app launch retrieve the language previously selected and apply it to the application.

when user changes the language, in order to apply the new laguage activity should be finished and restarted or else you need to call recreate() method of the activity.

https://github.com/gunhansancar/ChangeLanguageExample/blob/master/app/src/main/java/com/gunhansancar/changelanguageexample/helper/LocaleHelper.java

Copy the above class to your code.

In your activity override the below method.

@OverrideprotectedvoidattachBaseContext(Context base) {
        super.attachBaseContext(LocaleHelper.onAttach(base));
    }

When user change the language ,e.g its on button click. save the language using below code. Here en is the language (iso) code for english.

@OverridepublicvoidonClick(View v) {
     LocaleHelper.persist(context,"en");
     recreate();
}

Solution 2:

By default the language of app will be falling back to device locale as a result each time you restart your app the language will change.

In order to make your app's language same as user's choosen preference, you need to persist the language chosen by user. One way is to store the language preference in SharedPreference. When the app is launched, check for the value in preference to decide which Language should be used and then adjust your view accordingly.

You can do it as follows

Store value as follows when language is changed:

SharedPreferences.Editor editor = getSharedPreferences("MyPref", MODE_PRIVATE).edit(); editor.putString("language", "en"); editor.commit();

Retrieve value as follows in onCreate():

SharedPreferencesprefs= getSharedPreferences("MyPref", MODE_PRIVATE); 
StringselectedLanguage= prefs.getString("language", "<enter default locale of app>");
setLocale(selectedLanguage);

Eventually set the locale as follows:

Without Activity restart:

Set android:configChanges="locale" in your in manifest

Change your setLocale as follows:

privatevoidsetLocale(String lang) {
    LocalemyLocale=newLocale(lang);
    Resourcesres= getResources();
    DisplayMetricsdm= res.getDisplayMetrics();
    Configurationconf= res.getConfiguration();
    conf.locale = myLocale;
    res.updateConfiguration(conf, dm);
    getBaseContext().getResources().updateConfiguration(conf, getBaseContext().getResources().getDisplayMetrics());
    invalidateOptionsMenu();
    onConfigurationChanged(conf);//Add this line
}

Override onConfigurationChanged():

@OverridepublicvoidonConfigurationChanged(final Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    textView.setText(<your-text>);
    //Any other UI text to change
}

Alternatively you can call recreate(); once locale is changed. This will ensure your locale is changed correctly.

Post a Comment for "How To Make Stay In The Last Language When Android App Is Closed?"