7

I want to programmatically change the language.

So I have built two xml files.

values-it
-->string.xml

values-en
-->string.xml

This is the code in MainActivity to change the language of the whole application:

//ITALIAN

Resources res = getApplicationContext().getResources();
DisplayMetrics dm = res.getDisplayMetrics();
android.content.res.Configuration conf = res.getConfiguration();
conf.locale = new Locale("it");
res.updateConfiguration(conf, dm);

//ENGLISH

Resources res2 = getApplicationContext().getResources();
DisplayMetrics dm2 = res2.getDisplayMetrics();
android.content.res.Configuration conf2 = res2.getConfiguration();
conf2.locale = new Locale("en");
res2.updateConfiguration(conf2, dm2);

Now if I set the English language (for example) the code is executed with no error, but the label doesn't not change its text.
If I change the orientation of my device, the label changes its text correctly.

Now how can I modify my code to automatically refresh the label?

4

5 Answers 5

10
 AlertDialog.Builder builder = new AlertDialog.Builder(DashboardActivity.this);
            builder.setTitle(R.string.languages);
            // Add the buttons
            builder.setPositiveButton(R.string.english, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    String languageToLoad = "en"; // your language
                    Locale locale = new Locale(languageToLoad);
                    Locale.setDefault(locale);
                    Configuration config = new Configuration();
                    config.locale = locale;
                    getBaseContext().getResources().updateConfiguration(config,
                            getBaseContext().getResources().getDisplayMetrics());
                    dialog.dismiss();
                    rEditor.putString("language", languageToLoad);
                    rEditor.commit();



                    Intent refresh = new Intent(DashboardActivity.this, DashboardActivity.class);
                    startActivity(refresh);
                    finish();

                }
            });
            builder.setNegativeButton(R.string.gujarati, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // User cancelled the dialog

                    String languageToLoad = "gu"; // your language
                    Locale locale = new Locale(languageToLoad);
                    Locale.setDefault(locale);
                    Configuration config = new Configuration();
                    config.locale = locale;
                    getBaseContext().getResources().updateConfiguration(config,
                            getBaseContext().getResources().getDisplayMetrics());
                    dialog.dismiss();
                    rEditor.putString("language", languageToLoad);
                    rEditor.commit();


                    Intent refresh = new Intent(DashboardActivity.this, DashboardActivity.class);
                    startActivity(refresh);
                    finish();

                }
            });

            builder.create().show();

you have to reload activity to show new language text means restart.

Sign up to request clarification or add additional context in comments.

5 Comments

what is rEditor ?
sharedPreference Editor for saving data.developer.android.com/training/basics/data-storage/…
resource.updateConfiguration is deprecated. Now what is alternative?
How to check language from SharedPreferences and change language on app start up for not restarting app on start up? At what place it should be done?
5

you need to refresh your activity to load resources which it does incase of changing the orientation. try this

private void restartActivity() { 
 Intent intent = getIntent(); 
 finish(); 
 startActivity(intent);
}

1 Comment

Wouldn't a call to recreate() be simpler than writing a custom method?
3

If you have not solved with your problem yet, you can try this. It works for me, hope so it will help you. You can refer it in future

mSwitch = (Switch) findViewById(R.id.language_switch);

    mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if (isChecked){
                    mSwitch.setTextColor(Color.GRAY);
                    String languageToLoad = "es";
                    Locale locale = new Locale(languageToLoad);
                    Locale.setDefault(locale);
                    Configuration configuration = new Configuration();
                    configuration.setLocale(locale);
                    Toast.makeText(getApplicationContext(), "spanish is set", Toast.LENGTH_SHORT).show();
                    getBaseContext().getResources().updateConfiguration(configuration, getBaseContext().getResources().getDisplayMetrics());
                } else {
                    String languageToLoad = "en";
                    Locale locale = new Locale(languageToLoad);
                    Locale.setDefault(locale);
                    Configuration configuration = new Configuration();
                    configuration.setLocale(locale);
                    Toast.makeText(getApplicationContext(), "english is set", Toast.LENGTH_SHORT).show();
                    getBaseContext().getResources().updateConfiguration(configuration, getBaseContext().getResources().getDisplayMetrics());
                }
        }
    });

Comments

2

Best way would be to put all TextView.setText() in a method. Call that method in your onResume(); to set it the first time. Then recall that method when you have reset the language. (the Activity goes trough the onStart() onResume() etc when you change orientation)

3 Comments

it's not just about the strings ... sometimes directions are important too, RTL or LTR
Good call, this seems a good way to deal with it: stackoverflow.com/questions/10964488/…
actually dug around a bit more and found the correct solution, and put it in a gist: gist.github.com/muhammad-naderi/…
0

This is how we do:

com.android.internal.app.LocalePicker.updateLocales(LocaleList locales)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.