RTL layout bug in android Oreo

后端 未结 2 1612
刺人心
刺人心 2021-02-06 07:50

Ever since I upgraded to android oreo on mobile my RTL support for application is not working. it\'s changing the strings to Arabic but not changing layout direction. but if I r

2条回答
  •  梦谈多话
    2021-02-06 08:26

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

    /**
     * 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;
    }
    

提交回复
热议问题