Reputation: 377
I want to update app locale on language selection.
MainActivity:
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(LocaleHelper.onAttach(context));
}
Fragment in main activity to select language. On selection I have wrote below code:
private View.OnClickListener updateLanguage() {
return (View v) -> {
String code = v.getTag() != null ? v.getTag().toString() : getString(R.string.en_code);
LocaleHelper.setLocale(getContext(), code);
navController.navigate(R.id.action_languageFragment_to_instructionFragment);
};
}
On selection of language I am updating locale using below code:
Locale locale = new Locale(language);
Locale.setDefault(locale);
Resources resources = context.getResources();
Configuration config = resources.getConfiguration();
config.setLocale(locale);
return context.createConfigurationContext(config);
After selection redirects to next screen, but language is not getting updated. It is updated if I re-launch app.
How can I fix this?
Upvotes: 0
Views: 52
Reputation: 629
this code works fine with me
Resources resources = getResources();
DisplayMetrics dm = resources.getDisplayMetrics();
Configuration config = resources.getConfiguration();
if (Build.VERSION.SDK_INT < 17){ config.locale = new Locale(localecode.toLowerCase()); }
else { config.setLocale(new Locale(localecode.toLowerCase())); }
resources.updateConfiguration(config, dm);
Upvotes: 1