Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RTL layout bug in android Oreo

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;
}
like image 216
Usman Ghauri Avatar asked Sep 19 '17 09:09

Usman Ghauri


Video Answer


1 Answers

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);
like image 114
amorenew Avatar answered Oct 03 '22 17:10

amorenew