Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set-up the application Language in Android Preferences

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?

like image 868
vallllll Avatar asked Jun 20 '11 15:06

vallllll


2 Answers

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

like image 112
vallllll Avatar answered Sep 22 '22 20:09

vallllll


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.

like image 41
bytebender Avatar answered Sep 23 '22 20:09

bytebender