When i'm click on my drawer toggle i'm get the following exception
java.lang.IllegalArgumentException: No drawer view found with gravity LEFT
This is my activity_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"/>
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<fragment
android:id="@+id/navigation"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:name="com.xyz.ui.navigation.NavigationFragment"
tools:layout="@layout/fragment_navigation" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
My fragment_navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start">
</ListView>
And my list item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/navigation_item_text"
android:layout_gravity="center_horizontal" />
</LinearLayout>
Ok. Let me make the things simple and clear here.
What is
DrawerLayout
?https://developer.android.com/reference/android/support/v4/widget/DrawerLayout.htmlDrawerLayout
Acts as a top-level container for window content that allows for interactive "drawer" views to be pulled out from the edge of the window. Drawer positioning and layout is controlled using theandroid:layout_gravity
attribute on child views corresponding to which side of the view you want the drawer to emerge from: left or right. (Or start/end on platform versions that support layout direction.) To use aDrawerLayout
, position your primary content view as the first child with a width and height ofmatch_parent
. Add drawers as child views after the main content view and set thelayout_gravity
appropriately. Drawers commonly usematch_parent
for height with a fixed width.What is
DrawerLayout
?, In Simple words:android:layout_gravity="start"
ORandroid:layout_gravity="left"
Techie Stuff:
toggle()
functionActionBarDrawerToggle.java (android-sdk\sources\android-22\android\support\v7\app\ActionBarDrawerToggle.java)
Gravity.java (android-sdk\sources\android-22\android\view\Gravity.java)
Toggle()
function calls themDrawerLayout.openDrawer(Gravity.START)
DrawerLayout.java (support-v4-22.1.1.jar library)
layout_gravity="start”
orleft
theNo drawer view found with gravity LEFT
is thrown.Solution:
Are you using right to left(RTL) layout? Setting gravity left on RTL layout would throw this exception. This can be fixed by setting gravity start instead of left
From documentation
The "Android Drawer" needs to be a direct child node of the "DrawerLayout".
In your example above (re: original question example), you only have a single direct child node under "DrawerLayout" ("LinearLayout"), and no drawer view after it.
Move your drawer view out of your LinearLayout and place it after it.
java.lang.IllegalArgumentException: No drawer view found with gravity LEFT
SOLUTION Assign a layout_gravity = "start" or "left" attribute to one of your DrawerLayout child view if your drawer layout already have a child view. OR Simply create a child view inside your DrawerLayout View and give it a layout_gravity = "start" or "left" attribute. for example
You should use the same gravity in DrawerLayout and NavigationView: for example: tools:openDrawer="right"in DrawerLayout tag and android:layout_gravity="right" in NavigationView tag