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 ?