23

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 want to save it so when the user restart the app he will find the last choosed language.

Firas Al Mannaa
  • 916
  • 1
  • 11
  • 30

4 Answers4

38

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

Abol3z
  • 480
  • 4
  • 10
  • Will this change lang in entire android system or it will change for the application only? Am asking this so that when user leaves the application, they will be able to proceed with android default lang – Karue Benson Karue Apr 29 '17 at 04:54
  • @KarueBensonKarue it will change in application only. – Shahzain ali May 01 '17 at 12:53
7

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();
GuirNab
  • 193
  • 3
  • 7
  • 2
    According to the [documentation](https://developer.android.com/reference/android/app/Application.html#onCreate()) the Application onCreate() is called only on app start. So you're not really changing the language in runtime, but on next app start, right? Edit: Re-read the original question, your way solved the problem after all. – Micha Apr 06 '17 at 09:16
5

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.

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
  • 1
    do all developer uses this way in there apps, I feel its not very clear ! – Firas Al Mannaa Sep 01 '12 at 19:14
  • 1
    I can't speak for other developers as I don't have access to their codem – Raghav Sood Sep 01 '12 at 19:17
  • 1
    Well yes, this is the recommended and cleaner way to pass info that you have in one Activity to other activities. The other, more hacky / unproper way to do it is to keep the data as a static field in the Activity, and access it from other activities, but if someone asks, I didn't tell you that! :) – Shivan Dragon Sep 01 '12 at 19:22
  • sorry, but I mean is this an official way. cause I need the best way to do that. if you know please tell me – Firas Al Mannaa Sep 01 '12 at 19:22
  • 1
    I did. This is a nice, official and clean API you can use to persist data across activities and app restarts. – Raghav Sood Sep 01 '12 at 19:23
2
btnChange.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mContext);
            //preferences.edit().putString("lang", "bn").commit();

            String lang = 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");
            }else if(lang.equalsIgnoreCase("bn")){
                setLocale("en");
                preferences.edit().putString("lang", "en").commit();
                btnChange.setText("বাংলা");
            }
        }
    });


public void setLocale(String lang) {

    myLocale = new Locale(lang);
    Resources res = getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    conf.locale = myLocale;
    res.updateConfiguration(conf, dm);
    Intent refresh = new Intent(this, MainActivity.class);
    startActivity(refresh);
    finish();
}

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

Zahidul
  • 389
  • 5
  • 15