Question

Sorry, the title seems a bit unclear.

I have Base_Activity class.
In that class I have a menu that has a spinner with the list of languages to change the content of the application in different languages.
I have three activities.
I want to make it possible for the users to change the language from anywhere in the application (any activity).
I succeed to change the language, but the problem here is to refresh the current activity when the user wants to change the language from there.

public void setLocale(String currentLanguage) {

    myLocale = new Locale(currentLanguage);
    Resources res = getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    conf.locale = myLocale;
    res.updateConfiguration(conf, dm);
    Intent refresh = new Intent(//HERE HOW IS POSSIBLE SPECIFY THREE ACTIVITIES);
    startActivity(refresh);
}
Was it helpful?

Solution

You can use your Activity method "recreate", but I think it requires API level 11, like this:

public void setLocale(String currentLanguage) {

    myLocale = new Locale(currentLanguage);
    Resources res = getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    conf.locale = myLocale;
    res.updateConfiguration(conf, dm);
    recreate ();
}

Or if this is a Locale update you use this:

        Locale locale = new Locale(AR_LANG);
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());

OTHER TIPS

One way to do it is probably to finish() and restart the activity, the same way android destroy it on a configuration change.

finish();
Intent i = new Intent( this, this.getClass() );
startActivity( i );

the down side of this solution is that you are actually creating a whole new activity and the user will see a black screen for a little bit.

An other solution that could work is keeping a container as the contentView of your activity and recareate yourself the whole View. The down side here is that your code and View hiearchy will be strongly influenced by this solution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top