Fixed RTL error in android Oreo

Ever since I upgraded to android oreo on a mobile phone, my RTL support for the application does not work. it changes the lines to Arabic, but does not change the direction of the layout. but if I run the same RTL transition to any device lower than oreo, everything works fine. Has anyone else experienced this problem? Is there any official expression about this error and workaround?

Below is my method of changing the locale

public static boolean setDefaultLocale(Context context) { Resources resources = context.getResources(); PreferenceManager preferenceManager = PreferenceManager.getInstance(); String localLanguage = resources.getConfiguration().locale.getLanguage(); boolean isLanguageChanged = !preferenceManager.getCurrentLanguageCode().equalsIgnoreCase(localLanguage); if (isLanguageChanged) { Log.d("", preferenceManager.getCurrentLanguageCode()); Locale locale = new Locale(preferenceManager.getCurrentLanguageCode()); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) Locale.setDefault(Locale.Category.DISPLAY, locale); else Locale.setDefault(locale); Configuration config = new Configuration(); config.locale = locale; resources.updateConfiguration(config, resources.getDisplayMetrics()); ((Activity) context).recreate(); } return isLanguageChanged; } 
+5
source share
2 answers

Thanks to @amorenew and tweaked the method in the Util class to support this strange update in oreo below, this is a working method that you just have to call this method onResume whenever the user preference is for the user

 /** * this to change app language to the saved language in user preferences * * @param context * @return */ public static boolean setDefaultLocale(Context context, boolean isClearData) { Resources resources = context.getResources(); Resources resourcesApp = context.getApplicationContext().getResources(); String localLanguage = resources.getConfiguration().locale.getLanguage(); boolean isLanguageChanged = !PreferenceManager.getInstance().getCurrentLanguageCode().equalsIgnoreCase(localLanguage); if (isLanguageChanged) { Log.d("", PreferenceManager.getInstance().getCurrentLanguageCode()); Locale locale = new Locale(PreferenceManager.getInstance().getCurrentLanguageCode()); Locale.setDefault(locale); Configuration config = new Configuration(); config.locale = locale; resources.updateConfiguration(config, resources.getDisplayMetrics()); resourcesApp.updateConfiguration(config, resources.getDisplayMetrics()); //for API 25 Configuration configuration = resources.getConfiguration(); configuration.setLocale(locale); context.getApplicationContext().createConfigurationContext(configuration); context.createConfigurationContext(configuration); ((Activity) context).recreate(); if (isClearData) { CurrencyViewModel.getInstance().removeModel(); CarNationalityViewModel.getInstance().removeModel(); DialCodeViewModel.getInstance().removeModel(); } if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { ((Activity)context).getWindow().getDecorView().setLayoutDirection(Locale.getDefault().getLanguage().equalsIgnoreCase("ar") ? View.LAYOUT_DIRECTION_RTL : View.LAYOUT_DIRECTION_LTR); } } return isLanguageChanged; } 
+3
source

A simple fix in your onCreate function adds the following code:

 if (Locale.getDefault().getLanguage()=="ar") getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL); else getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_LTR); 
+4
source

Source: https://habr.com/ru/post/1271925/


All Articles