1

I would like to change the application language in app level. I have values-fr/string.xml, values-cn/string.xml and values-en/string.xml.

In my app Java code, I have a dialog which shows a list of language options, and I try to use the following code to change the language of my app:

//user select a language from the dialog list
language_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {


             String language="";
             switch (position) {
                case 0:
                    language="fr";
                    break;
                case 1:
                    language="cn";
                    break;
                case 2:
                    language="en";
                    break;
             }

             Locale locale= new Locale(language);
             Locale.setDefault(locale);

             Configuration config= new Configuration();
             config.locale=locale;
             context.getResources().updateConfiguration(config,context.getResources().getDisplayMetrics()); 

             dissmissDialog();
        }
    });

I think with the above code, after user selected a language, dialog get dismissed, the application should load the string according to the selected locale (e.g. values-fr/strings.xml). But my app string does not change after dialog dismissed.

I guess my Java code should tell the app to reload the configuration, and do refresh, but I do not know how to do that in Android.

So, normally, how to change locale in application level so that after user selected a language, the app can refresh and show the latest chosen locale strings ?

Leem.fin
  • 40,781
  • 83
  • 202
  • 354
  • Check out this earlier post: http://stackoverflow.com/questions/2900023/change-language-programatically-in-android – jsmith Mar 27 '12 at 11:06
  • @jsmith, Yes, I checked the link you provided, but it only change the language when user go to the next screen, the current screen does not refreshed to use the new locale. How to get rid of this problem? – Leem.fin Mar 27 '12 at 11:25
  • @Leem.fin do u still have the problem, if so, the less painful way to do this is calling setContentView(R.layout.screenname) and setting all listeners once again. – Ganesh K Jul 06 '12 at 06:06
  • @Leem.fin although this is a 2012 post, I just want to add a comment and hope can help others. `recreate()` can be used for this purpose. – Carson Ip Apr 11 '14 at 13:47

0 Answers0