14

In my application, I have implemented some languages including Arabic language which starts from Right to Left. I want to align all icons in the Toolbar be RIGHT_TO_LEFT.

I tried layout_gravity="right" and gravity="right" But nothing happed.

My Toolbar code:

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/primary_color"
    app:popupTheme="@style/Theme.AppCompat.Light"/>

Here is what I want:


3 Answers 3

23

Official right-to-left support was introduced with Android 4.2 (API level 17), so this will only work if your minimum SDK level is 17:

1) Put the android:supportsRtl="true" attribute in your AndroidManifest file to support right-to-Left layout directions.

2) Secondly put this code in your activity's onCreate() methdod:

if (getWindow().getDecorView().getLayoutDirection() == View.LAYOUT_DIRECTION_LTR){
            getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
        }

This will make your everything is placed from right to left if the system uses an according language. Also make sure you use start/end instead of left/right when setting up padding to make sure that all elements are aligned correctly.

For more information about the official support announcement: http://android-developers.blogspot.be/2013/03/native-rtl-support-in-android-42.html

2
  • Thanks, that worked. However the required API limited me for not use it because many devices still have lower API's. also Upvoted ;) Commented Mar 28, 2015 at 14:01
  • for toolbar this works only if I restart the app. Have been using the same for other view and it does work without app restart. . Commented Nov 26, 2018 at 19:38
6

Put the android:supportsRtl="true" attribute in your AndroidManifest file. After that add below line to your toolbar tag:

android:layoutDirection="rtl"
1

For those who are looking to have only the hamburger icon to right and have only the icon in toolbar, you can use

android:layoutDirection="rtl"

on the toolbar tag

Not the answer you're looking for? Browse other questions tagged or ask your own question.