0

I'm looking for a way to implement change of language triggered by button onclick. I have created proper strings.xml in proper folders (for example res/values-en/) Here is my code of the button:

Button setEN_bt = (Button) findViewById(R.id.setEN);        
setEN_bt.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Locale locale_en = new Locale("en"); 
            Locale.setDefault(locale_en);
            Configuration config_en = new Configuration();
            config_en.locale = locale_en;
            getBaseContext().getResources().updateConfiguration(config_en, getBaseContext().getResources().getDisplayMetrics());
        }
    });

So basicly I change the locale with the onclick but the page doesn't refresh afterwards (why should it...:) ). Could somebody tell me how to refresh/reload the activity?

Thanx for help in advance.

baron_bartek
  • 1,073
  • 2
  • 20
  • 39

4 Answers4

1

You may try to call

view.invalidate();

on the view that you want redraw itself.

I'm not sure, if this will reload the Locales. If not, you could restart the Activity by creating a new Intent for itself:

Intent intent = new Intent(this, Activity.class);
startActivity(intent);

Cheers

Florian Barth
  • 1,392
  • 12
  • 25
  • Thx for reply. I used this: v.invalidate() but it didnt work, so I tried with Intend but it wont work too. It seems that I can't call "this." inside the methods of the button listener. Any clue? Maybe I can pass it somehow into the method. – baron_bartek Oct 30 '12 at 14:06
  • 1
    try using .this if you are in a listener. For example "new Intent(CurrentActivity.this, NewActivity.class)". – Florian Barth Oct 30 '12 at 14:14
  • THANX Florian it worked. Here is the code: Intent intent = new Intent(InvestWroclawActivity.this, InvestWroclawActivity.class); startActivity(intent); – baron_bartek Oct 30 '12 at 14:33
  • @Florian Barth do you know how to be in fragments with navigation drawer? – Sasha Dec 22 '14 at 13:37
  • What do you mean with "how to be in fragments", Sultan? – Florian Barth Dec 22 '14 at 14:09
1

You can just start the activity by using

startActivity(new Intent(CurrentActivity.this, CurrentActivity.class)); finish(); and finish the previous activity by using finish() method at the end in onclick method.

Sidharath
  • 130
  • 1
  • 7
1

Instead of calling startActivity() and then finish(), you can call:

recreate(); // which is available from API Level 11.

I found this answer here which has another great ways to do it for lower API Levels: How do I restart an Android Activity

Community
  • 1
  • 1
Juan Saravia
  • 7,661
  • 6
  • 29
  • 41
0

First Method

     String languageToLoad  = "en";
     Locale locale = new Locale(languageToLoad); 
     Locale.setDefault(locale);
     Configuration config = new Configuration();
     config.locale = locale;
     context.getResources().updateConfiguration(config,context.getResources().getDisplayMetrics());

     Intent intent = new Intent(XYZ.this, XYZ.class);
     intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//it will recreate it self with new language.
     startActivity(intent);

You need to restart your Activity after changing locale.

Second Method

 mLocale = new Locale("en");
          Locale.setDefault(mLocale); 
          Configuration config = new Configuration();
          config.locale = mLocale;
          getBaseContext().getResources().updateConfiguration(config, 
          getBaseContext().getResources().getDisplayMetrics());
          MainActivity.this.setContentView(R.layout.activity_main);//reset layout 

but it will work on api>=11

Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300