Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : Change entire app layout directions programmatically

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?

like image 489
Hamidreza Salehi Avatar asked Aug 09 '15 13:08

Hamidreza Salehi


People also ask

How to set layout direction programmatically in android?

Just go to Android Studio > Refactor > Add RTL support where possible… I would recommend you checking your app once after applying this change as you might not want all your Layouts/Views to be RTL. If you want to force any layout to LTR then just add android:layoutDirection="ltr" to that view.

What does Force RTL layout direction do?

Once you've accessed the menu, scroll down to find the “Force RTL layout direction” tab. Tap on it to activate the option, and you're done. All system menus and Google apps will reflect the change: phonebook, messaging, Google Play, Gmail, notifications area, and so on.

How to set RTL in android?

In your android phone, tap on “Settings” icon. Now tap on developer options and search for “Force RTL layout direction”. Tap on it to enable RTL feature.


1 Answers

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()); 
like image 102
Afshin Avatar answered Oct 02 '22 08:10

Afshin