'Android Navigation Drawer RTL crash

I'm using appcompact for navigation drawer, everything is working perfectly as following, but when i try to change the drawer from RTL using android:layout_gravity="start" the app compiles but on the click of the icon it crashes any idea what's causing the crash and how to make it RTL

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="64dp"
    android:background="@color/color_primary"
    android:minHeight="?attr/actionBarSize" />

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start">
    <!-- Main layout -->
    <FrameLayout
        android:id="@+id/main_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <!-- The navigation drawer -->
    <ListView android:id="@+id/left_drawer"
              android:layout_width="240dp"
              android:layout_height="match_parent"
              android:layout_gravity="end"
              android:choiceMode="singleChoice"
              android:divider="@android:color/transparent"
              android:dividerHeight="0dp"
              android:background="#ccc"/>


</android.support.v4.widget.DrawerLayout>

here's the error log

Process: inducesmile.com.androidnavigationdrawer, PID: 15182
java.lang.IllegalArgumentException: No drawer view found with gravity LEFT
        at 


Solution 1:[1]

As you said, you're opening the navigation drawer by the "android:layout_gravity="start" attribute : That's is good for the RTL compatibility.

The thing is when the application is

  • LTR : START = LEFT and END = RIGHT
  • RTL : START = RIGHT and END = LEFT

So you should be careful when using LEFT and RIGHT attributes : we generally use them only when there is a view, text direction or many other attributes that should keep the same whatever the language you're using.

So your java exception says that your class is using Gravity.LEFT instead of Gravity.START :
So you are trying to open a drawer from the Gravity.LEFT side where on you layout said layout_gravity="start" it gonna open the right side on RTL : the two attributes must always be the same.

For example must change :

myDrawerLayout.openDrawer(Gravity.LEFT);

to :

myDrawerLayout.openDrawer(Gravity.START);

Update 03/2022

It seems for androidx packages, they are using the class GravityCompat instead of Gravity So instead of doing :

myDrawerLayout.openDrawer(Gravity.START);

You may need to do :

myDrawerLayout.openDrawer(GravityCompat.START);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1