Android how to change the application language at runtime

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

String languageToLoad  = "en"; // your language 
Locale locale = new Locale(languageToLoad);  
Locale.setDefault(locale); 
Configuration config = new Configuration(); 
config.locale = locale; 
getBaseContext().getResources().updateConfiguration(config,  
getBaseContext().getResources().getDisplayMetrics());

then you should save the language like this

SharedPreferences languagepref = getSharedPreferences("language",MODE_PRIVATE);
SharedPreferences.Editor editor = 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

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:

public class MyApplication extends Application {

   @Override
   public void onCreate() {
       super.onCreate();
       SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
       String lang = preferences.getString("lang", "en");
       Locale locale = new Locale(lang);
       Locale.setDefault(locale);
       Configuration config = new Configuration();
       config.locale = locale;
       getBaseContext().getResources().updateConfiguration(config,
               getBaseContext().getResources().getDisplayMetrics());    
   }  
}

And set MyApplication as application class in manifest:

 <application
        android:name=".MyApplication"
        ...
 />

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

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

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.

Leave a Comment