Search code examples
androidandroid-toolbar

Align Toolbar icons from right to left?


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:



Solution

  • 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