RTL layout bug in android Oreo

浪子不回头ぞ 提交于 2019-12-04 09:56:58

问题


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 run same RTL shift to any of device lower than oreo, everything works fine. anyone else experienced this issue? is there any official statement yet about this bug and workaround?

Below is my method to change 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;
}

回答1:


Simple fix in your onCreate function add the following code:

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



回答2:


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


来源:https://stackoverflow.com/questions/46296202/rtl-layout-bug-in-android-oreo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!