8

I would like the application language to be set according to the user preferences but up till now it doesn't work how I would like it to.

I have set the default values: strings.xml and also values-es with a strings.xml inside in spanish. I have a menu option which brings the user to a Preference activity where he can amon gother things chose the language.

So here are some extracts of the code:

public class Preference extends PreferenceActivity implements OnSharedPreferenceChangeListener { ...... // Set up a listener whenever a key changes getPreferenceScreen().getSharedPreferences() .registerOnSharedPreferenceChangeListener(this); ...} //(......) //and here I have the listener so when the language pref changes value the locale gets changed. @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { if (key.equals("listPref2")) { String idioma = sharedPreferences.getString("listPref2", "catala"); if ("castella".equals(idioma)) { idioma = "es_ES"; Locale locale = new Locale(idioma); Locale.setDefault(locale); Configuration config = new Configuration(); config.locale = locale; getApplicationContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics()); } } } 

So when I change the language it works but then when I come back later or restart the emulator the language gets back to default locale the en_US and the app language gets changed back to default again. What can I do to sort that out?

I know I can get this preference (which I can access to from all my activities) and then each time set up the locale but I find it a bit heavy isn't there a way to do it in a more elegant way?

What I would like to do is if the user sets up the language so when he comes back 2 days later he doesn't have to change the language again.

Any ideas?

2 Answers 2

11

OK it may help someone. I have added the folowing to the main activity manifest:

android:configChanges="locale"

Then when the user choses the preferences I have put a confirm button and then this button brings you to main activity that is why the lnagages gets reset.

I have a static class where I have this code to change the locale:

public static void updateLanguage(Context context, String idioma) { if (!"".equals(idioma)) { if ("castella".equals(idioma)) { idioma = "es_ES"; } else if ("catala".equals(idioma)) { idioma = "ca_ES"; } Locale locale = new Locale(idioma); Locale.setDefault(locale); Configuration config = new Configuration(); config.locale = locale; context.getResources().updateConfiguration(config, null); } } 

end at every activity I have like 20 of them I call this method before:

setContentView(R.layout.list_event);

With these methods when I rotate the screen the activities don't change the language here is a link to a blog that helped me: http://adrianvintu.com/blogengine/post/Force-Locale-on-Android.aspx

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

Comments

1

I would think that you need to be setting the locale in the MainActivity onCreate method. The same way you are setting it when the onSharedPreferenceChanged method.

3 Comments

I have done what you told but this changes the locale only in the main activity. I will just add the same code at the beginning of all my activities then. thanks
I have to admit I never attempted the local thing. Looks like it is a based on a user setting. have you looked at this? developer.android.com/resources/tutorials/localization/…
yes I have already set up a default langage and 2 languages: spanish and catalan and I need the user to be able to chose himself the locale. Determining the locale only with the phone is not enough.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.