0

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);
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
bShah
  • 309
  • 1
  • 6
  • 19

2 Answers2

2

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());
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
wanam
  • 548
  • 4
  • 8
  • **Or if this is a Locale update you use this:** As I have mentioned in question. Changing language is in menu in spinner that can be accessible from any activity of app. So this is in Base activity class. I do not have OnCreate() method in it. – bShah Feb 09 '14 at 11:10
  • I want to set currentLanguage in Singleton class also. – bShah Feb 09 '14 at 11:16
0

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.

Mario Lenci
  • 10,422
  • 5
  • 39
  • 50