Unable to change language in Oreo
When you set new Locale
you should restart your Activity
. You can perform it using the next snippet of code:
private void restartActivity() {
Intent intent = getIntent();
finish();
startActivity(intent);
}
Then your changeLanguage()
method will look in a next way:
public void changeLanguage(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Configuration config = context.getResources().getConfiguration();
config.setLocale(locale);
context.createConfigurationContext(config);
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
restartActivity();
}
I recently faced issue related to layout direction on Oreo
. Resources were being updated but Layout direction not changing.
Below code worked for me.
public static void setRTLSupportIfRequired(AppCompatActivity activity) {
if(getLanguageFromPrefs(activity).equals("ar")) {
activity.getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
}else{
activity.getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_LTR);
}
}
Note: Previously I were using View.LAYOUT_DIRECTION_LOCALE
but it was not working on Oreo