2

In my app i have a spinner to select language "en" or "ar".I have String.xml for both languages.
Once we select language from spinner i want my layout to change in language chosen.

I tried this but doesn't change when i select from spinner:

 Spinner languageSpinner=(Spinner) findViewById(R.id.languagespinner);
            languageSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {

                if(arg2==0)
                    languageToLoad  = "en"; // your language
                else
                    languageToLoad  = "ar";
                Locale locale = new Locale(languageToLoad);  
                Locale.setDefault(locale); 
                Configuration config = new Configuration(); 
                config.locale = locale; 
                getBaseContext().getResources().updateConfiguration(config,  
                getBaseContext().getResources().getDisplayMetrics());
                onConfigurationChanged(config);
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });

How can i do it?

Bhavin Nattar
  • 3,189
  • 2
  • 22
  • 30
Mario
  • 145
  • 2
  • 16

3 Answers3

1

After setting locale you need to reload your current activity

finish();
startActivity(getIntent());

will reload your activity.

saygun
  • 1,438
  • 12
  • 26
  • Since i have used spinner everytime i do this it infinitely finishes and restarts my activity – Mario Apr 08 '14 at 09:58
0

in your activity call this function to restart your activity

private void restartActivity() {
    Intent intent = getIntent();
    finish();
    startActivity(intent);
}
Syed Waqas
  • 862
  • 2
  • 9
  • 29
0

As of API level >11 you can use

recreate()

To recreate(new instance) your activity. Have a look at the documentation.

RaphMclee
  • 1,623
  • 13
  • 16