Android RTL issue in API 24 and higher on locale change

后端 未结 2 1037
独厮守ぢ
独厮守ぢ 2021-01-06 14:49

I was trying to change locale of app at runtime. It is working fine in Andorid below API level 24. But in API level 24 or greater the layout direction is not changing accord

2条回答
  •  花落未央
    2021-01-06 15:04

    The problem seems to be that it doesn't reflect layout direction changes at first update. I solved the problem by overriding the onAttachedToWindow method of Activity like below:

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            getWindow().getDecorView().setLayoutDirection(
                    "ur".equals(LocaleHelper.getLanguage(this)) ?
                    View.LAYOUT_DIRECTION_RTL : View.LAYOUT_DIRECTION_LTR);
        }
    }
    

    Tested on API 25 and it's working fine. Be careful though I'm not sure about any side effects for this approach at this moment. Nevertheless, I think it is what you are looking for.

    I'll also update the blog post to reflect more elegant, generic code for this.

提交回复
热议问题