Search code examples
androidxmluser-interfaceright-to-leftnavigation-drawer

Android right to left NavigationDrawer menu items aren't RTL


I need to align navigation menu items to the right. I read many articles and questions and answers but i couldn't find what's wrong in my project. This is my xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="right">

<include
    layout="@layout/content_activity_home"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="right"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_activity_home"
    app:menu="@menu/activity_home_drawer" />

This is activity_home_drawer.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<group android:checkableBehavior="single">
    <item
        android:id="@+id/menu_account"
        android:icon="@drawable/ic_profile"
        android:title="حساب کاربری" />
    <item
        android:id="@+id/menu_logout"
        android:icon="@drawable/ic_sign_out"
        android:title="خروج از حساب" />
</group>

I can make drawer itself to open from right but elements of NavigationView still remain left to right. You can see the result here:

enter image description here

As you see menu items aren't right to left. How to make them rtl?


Solution

  • try adding android:layoutDirection="rtl"

    <android.support.design.widget.NavigationView
    android:layoutDirection="rtl"
    android:id="@+id/navigation_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="right"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />
    

    This works for api level 17 or above. For older devices there is a trick. In application level of android manifest set supportRtl="false" and in layouts set layout_gravity="right". This works properly.