1

I want to rotate my phone and keep the locale. I have folders values, values-en and values-hr. In every activity I have

android:configChanges="keyboardHidden|orientation|locale" 

and in every activity.java file I have

private Locale locale = null; @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); if (locale != null) { newConfig.locale = locale; Locale.setDefault(locale); getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics()); } } 

and in onCreate

 SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this); Configuration config = getBaseContext().getResources().getConfiguration(); String lang = settings.getString("language","en"); if (! "".equals(lang) && ! config.locale.getLanguage().equals(lang)) { locale = new Locale(lang); Locale.setDefault(locale); config.locale = locale; getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics()); } 

It works till I rotate my phone - app is still working, buttons and everything is fine - but locale is getting weird - sometimes it's my locale (hr) sometimes it's (en)...

What else I need to add?

4
  • Why are you messing with the locale, as opposed to letting the android system handle it for you? Commented Jan 14, 2011 at 22:44
  • 1
    I believe this may be a duplicate question: stackoverflow.com/questions/3711712/…, Please correct me if I am wrong. Commented Jan 14, 2011 at 23:05
  • Please take a look at my solution: stackoverflow.com/a/9535374/498067 . It is very easy and doesn't require any changes in your Activities. Commented Mar 2, 2012 at 15:02
  • Aren't you forgetting to reset the contentView e.g setContentView(R.layout.mylayout)? Commented Apr 24, 2012 at 22:49

1 Answer 1

1

I simply removed "if's" and everything is working now. Yes, it is a little "bulky" every time to "re-force" a locale but I couldn't find another way.

My solution is a little bit different, I just "re-force" it on onCreate and onConfigChange - not every 100ms or so and on Desire (2.2) it's working flawlessly.

@Austyn

Yes, it's pretty much the same question but I though is there another solution except "100ms forcing".

@Mayra

Users wanna have their mother-tongue (HR) but it's not supported in (current) mobile phones so this is the only way - to force it =/

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.