4

Possible Duplicate:
Change app language programmatically in Android

I want to change language in my SettingsActivity.java for all Activities immediately.
And save this choice (even when I exit from app or reboot phone) regardless of system locale.
How can I do that?

public class MyOnItemSelectedListener implements OnItemSelectedListener {

    public void onItemSelected(AdapterView<?> parent,
        View view, int pos, long id) { 
        switch (pos) {
        case 0:
            Locale locale = new Locale("en");
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(config, getBaseContext()
                    .getResources().getDisplayMetrics());
            break;

        case 1: 
            Locale locale2 = new Locale("de");
            Locale.setDefault(locale2);
            Configuration config2 = new Configuration();
            config2.locale = locale2;
            getBaseContext().getResources().updateConfiguration(config2, getBaseContext()
                    .getResources().getDisplayMetrics()); 
            break;          
        }       
    }

    public void onNothingSelected(AdapterView<?> parent) {
      // Do nothing.
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
user1680782
  • 111
  • 4
  • 9

1 Answers1

2

1) between exit app and reboot you can save choice like this: Create Application class

public class MutawaApplication extends Application {

(http://developer.android.com/reference/android/app/Application.html) register it in AndroidManifest.xml

android:name=".MyApplication"

In this class in onCreate read language from SharedPreferences. Save it to SharedPreferences when you change lang in app.

2) in your Activities check locale on onResume and if need make replaceContentView()

anber
  • 3,463
  • 6
  • 39
  • 68