Android : Change entire app layout directions programmatically

androidandroid-actionbardirectionright-to-lefttoolbar

How can I change entire app layout direction to RTL?
I am writing an app that user must select it's language in first launch and the layout should change based on user selection to RTL or remains LTR.
I used to add android:supportsRtl="true" to the AndroidManifest and android:layoutDirection="rtl" for each layout but this approach have some problems like below :

One problem is when I change the direction to RTL the ActionBar home icon or navigation button (when home as up is enabled) remains LRT and just move to the right.

I also tried to change direction programmatically and the result was the same :

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
            getWindows().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
        }

enter image description here

Is there any way to force all activities to become RTL at once or we should set direction in each activity separately?

Best Answer

You can use this piece of code while your application's minSdk >= 17.

I used fa for Farsi, you can use other rtl language.

Configuration configuration = getResources().getConfiguration();
configuration.setLayoutDirection(new Locale("fa"));
getResources().updateConfiguration(configuration, getResources().getDisplayMetrics());