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);
}
Is there any way to force all activities to become RTL at once or we should set direction in each activity separately?
Following code will work:
where locale is object of Locale
Try code below if you want to change language in the app and change directions in views and resources on active layout:
Also, after changing the language, you need to change the directions for the already created views on the active layout. You can do this with this code:
You can use this piece of code while your application's minSdk >= 17.
I used
fa
forFarsi
, you can use other rtl language.add this class:
then use above class like this(in your activity):
and your layout will be RTL.
I also have this problem, i find that if your view's width set
MATCH_PARENT
, you should set the view's gravity withGravity.LEFT
in LTR model and set the view's gravity withGravity.RIGHT
in RTL model.Change the whole layout