12

Possible Duplicate:
Changing Locale within the app itself

in my application I need to "force" language let's say that I have locale in english as default polish and finnish, according to that post I had created function posted also bellow, function is called in createActivity(), but the problem is it does not work.... any idea why? Any suggestions?

private void setLocale(String localeCode){
        Locale locale = new Locale(localeCode);
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
    }
Community
  • 1
  • 1
Robert
  • 1,272
  • 4
  • 21
  • 40

2 Answers2

12

Here is what I got so far. I know this question is resolved, but my solution is easier and more compact. No other changes are needed, no android:configurationChanges attribute for all activities in your manifest.

public class SigmaMiddleEastApplication extends PPGApplication {

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        newConfig.locale = Locale.ENGLISH;
        super.onConfigurationChanged(newConfig);

        Locale.setDefault(newConfig.locale);
        getBaseContext().getResources().updateConfiguration(newConfig, getResources().getDisplayMetrics());
    }
}

BEWARE this may cause problems: What could cause an Android activity to relaunch itself infinitely when returning from camera?

Community
  • 1
  • 1
Sergii Rudchenko
  • 5,170
  • 2
  • 28
  • 24
  • 3
    Just wrap everything around a "if(newConfig.locale!=targetLocale)" should resolve the infinite relaunch problem. – Edison Feb 21 '13 at 03:59
3

Add the following in the manifest (for every activity) :

android:configChanges="locale"
researcher
  • 1,758
  • 22
  • 25
Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243