Skip to content Skip to sidebar Skip to footer

Android How To Change The Application Language At Runtime

I want to let the user change the language of my application using spinner (or any way). I tried many ways but they change the language of this activity not all activities, and I w

Solution 1:

you can use this code in spinner or any way you want

StringlanguageToLoad="en"; // your language Localelocale=newLocale(languageToLoad);  
Locale.setDefault(locale); 
Configurationconfig=newConfiguration(); 
config.locale = locale; 
getBaseContext().getResources().updateConfiguration(config,  
getBaseContext().getResources().getDisplayMetrics());

then you should save the language like this

SharedPreferenceslanguagepref= getSharedPreferences("language",MODE_PRIVATE);
SharedPreferences.Editoreditor= languagepref.edit();
editor.putString("languageToLoad",languageToLoad );
editor.commit();  

and use the same code in every activity in onCreate() to load the languageToLoad from the SharedPreferences

Solution 2:

This is an old question, but I'll answer it anyway :-) You can extend Application class to apply Abol3z's solution on every Activity. Create class:

publicclassMyApplicationextendsApplication {

   @OverridepublicvoidonCreate() {
       super.onCreate();
       SharedPreferencespreferences= PreferenceManager.getDefaultSharedPreferences(this);
       Stringlang= preferences.getString("lang", "en");
       Localelocale=newLocale(lang);
       Locale.setDefault(locale);
       Configurationconfig=newConfiguration();
       config.locale = locale;
       getBaseContext().getResources().updateConfiguration(config,
               getBaseContext().getResources().getDisplayMetrics());    
   }  
}

And set MyApplication as application class in manifest:

<applicationandroid:name=".MyApplication"...
 />

You can set the lang value (in your spinner):

SharedPreferencespreferences= PreferenceManager.getDefaultSharedPreferences(getContext());
preferences.edit().putString("lang", "en").commit();

Solution 3:

Use SharedPreferences to keep track of the language the user chose, and then set the activities to use that language in the onCreate (), and maybe onResume() method. This way it will persist across app restarts etc.

Solution 4:

btnChange.setOnClickListener(newView.OnClickListener() {
        @OverridepublicvoidonClick(View v) {
            SharedPreferencespreferences= PreferenceManager.getDefaultSharedPreferences(mContext);
            //preferences.edit().putString("lang", "bn").commit();Stringlang= preferences.getString("lang", "en");
            //Log.e("lang", "lang in Main Activity:"+lang);if (lang.equalsIgnoreCase("en")){
                setLocale("bn");
                preferences.edit().putString("lang", "bn").commit();
                btnChange.setText("Eng");
            }elseif(lang.equalsIgnoreCase("bn")){
                setLocale("en");
                preferences.edit().putString("lang", "en").commit();
                btnChange.setText("বাংলা");
            }
        }
    });


publicvoidsetLocale(String lang) {

    myLocale = newLocale(lang);
    Resourcesres= getResources();
    DisplayMetricsdm= res.getDisplayMetrics();
    Configurationconf= res.getConfiguration();
    conf.locale = myLocale;
    res.updateConfiguration(conf, dm);
    Intentrefresh=newIntent(this, MainActivity.class);
    startActivity(refresh);
    finish();
}

we use two language for test purpose. keep all string in different folder named values and values-bn.

Post a Comment for "Android How To Change The Application Language At Runtime"