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?

bircastri
  • 2,169
  • 13
  • 50
  • 119
  • try to call your code in onResume(). – Mehta May 26 '16 at 07:46
  • 1
    Hi, here you are a link telling you how to reload changes without closing the activity: http://stackoverflow.com/questions/2644377/changing-locale-force-activity-to-reload-resources – Miguel Benitez May 26 '16 at 07:53
  • Possible duplicate of [How to refresh activity after changing language (Locale) inside application](http://stackoverflow.com/questions/8049207/how-to-refresh-activity-after-changing-language-locale-inside-application) – Jofre Mateu May 26 '16 at 07:54
  • conf2.locale and res2.updateConfiguration() are deprecated. :S – baquiax Mar 21 '17 at 17:26

5 Answers5

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.

Ramchandra Singh
  • 530
  • 4
  • 15
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);
}
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());
                }
        }
    });
Fazeel Qureshi
  • 854
  • 9
  • 18
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)

0

This is how we do:

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

Zbigniew Mazur
  • 653
  • 7
  • 11