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
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.