views:

269

answers:

2

So I have a language setting in my application. When the language is switched, I would like all the textviews etc to change language immediately. Currently I just change the locale in the configuration, so the language has changed when the user restarts the activity.

An ugly solution to my problem would be to make each textview load the new resources each time the language is changed. Is there a better solution? Perhaps a neat way to discretely restart the activity? Or maybe just force reload of the resources?

Cheers,

+1  A: 

In your AndroidManifest.xml, add this attribute to your Activity

android:configChanges="locale"

In your activity override onConfigurationChanged()

@Override
public void onConfigurationChanged(Configuration newConfig) {
  // refresh your views here
  super.onConfigurationChanged(newConfig);
}

http://developer.android.com/intl/de/guide/topics/manifest/activity-element.html#config

Jim Blackler
Is this supposed to trigger when I call resource.updateConfiguration()? Im currently not having any success with getting the onConfigurationChanged() to run. :'(
sandis
onConfigurationChanged() should trigger when the language is changed.Actually just looking back there is a red flag in your question which I missed first time, "I have a language setting in my application". Does this mean you are not using the Android device-wide language setting facility?
Jim Blackler
Im not sure what you mean? But I do use the locale of the phone when the app first starts, and I have several resources marked for example values-en, values-fr, values-de etc. I just still want to the user to be able to have my app in a different language then the phone. I dont know why, but some users have requested this feature.
sandis
Aren't users annoying? I often think we'd be better off without them :-) Anyway if you have rolled your own language selector, why are you having difficulty refreshing your layouts?
Jim Blackler
True :P I am not using my completely own language support. Im still using resource.updateConfiguration(), and Im not sure why this isnt picked up by onConfigurationChanged().
sandis
A: 

im using this and its not working, i have a preference activity and i have a listpreference to change between languages. It works but only when i navigate to anoter activity through the startActivity method. If i go back with the back arrow button they still show the old language.

This is the preferenceActivity:

public class Settings extends PreferenceActivity{
final static int PORTUGUESE=1;
final static int ENGLISH=2;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.settings);

    Preference langPref=(Preference)findPreference("listPref");
    langPref.setOnPreferenceChangeListener(
            new OnPreferenceChangeListener(){


                @Override
                public boolean onPreferenceChange(Preference preference,
                        Object newValue) {

                    String lanfpref=(String) newValue;
                    int lang=Integer.parseInt(lanfpref);

                    Resources res = getResources();
                    DisplayMetrics dm = res.getDisplayMetrics();
                    Configuration conf = res.getConfiguration();

                    switch(lang){
                    case PORTUGUESE:
                        conf.locale = Locale.ITALY;
                        break;
                    case ENGLISH:
                        conf.locale = Locale.ENGLISH;
                        break;
                    }
                    res.updateConfiguration(conf, dm);
                    return true;
                }
            });

}

}

The method onConfigurationChaged is on the other activity that calls this settings class.

Maxrunner