Android DrawerLayout - No drawer view found with gravity left

2

When you click on the button to display the DrawerLayout, you get this error:

E/AndroidRuntime: FATAL EXCEPTION: main

                                               java.lang.IllegalArgumentException: No drawer view found with gravity LEFT
                                                   at android.support.v4.widget.DrawerLayout.openDrawer(DrawerLayout.java:1648)
                                                   at android.support.v4.widget.DrawerLayout.openDrawer(DrawerLayout.java:1634)
                                                   at android.support.v7.app.ActionBarDrawerToggle.toggle(ActionBarDrawerToggle.java:290)
                                                   at android.support.v7.app.ActionBarDrawerToggle.access$100(ActionBarDrawerToggle.java:64)
                                                   at android.support.v7.app.ActionBarDrawerToggle$1.onClick(ActionBarDrawerToggle.java:200)
                                                   at android.view.View.performClick(View.java:4761)
                                                   at android.view.View$PerformClick.run(View.java:19767)
                                                   at android.os.Handler.handleCallback(Handler.java:739)
                                                   at android.os.Handler.dispatchMessage(Handler.java:95)
                                                   at android.os.Looper.loop(Looper.java:135)
                                                   at android.app.ActivityThread.main(ActivityThread.java:5310)
                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                   at java.lang.reflect.Method.invoke(Method.java:372)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

I need the nav drawer to come from the right.

The code is so activity_nav_drawer_example.xml:

<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="end">

<include
    layout="@layout/app_bar_nav_drawer_example"
    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="end"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_nav_drawer_example"
    app:menu="@menu/activity_nav_drawer_example_drawer" />
</android.support.v4.widget.DrawerLayout>

app_bar_nav_drawer_example.xml:

<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.youngsolutions.tapptaximonitoreo.activities.NavDrawerExample">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_nav_drawer_example" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    app:srcCompat="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
    
asked by Lina Cortés 23.09.2016 в 21:57
source

2 answers

1

It is important that the element within DrawerLayout you must set your first item with width and height defined as match_parent and with layout_gravity set as LEFT or RIGHT, check the documentation .

change the property:

android:layout_gravity="end"

a:

android:layout_gravity="right"

This would be the corrected layout:

<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="end">

<include
    layout="@layout/app_bar_nav_drawer_example"
    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_nav_drawer_example"
    app:menu="@menu/activity_nav_drawer_example_drawer" />
</android.support.v4.widget.DrawerLayout>
    
answered by 24.09.2016 в 00:36
0

Over there it's a bit late, but I found this alternative in SO that worked for me. It is additional to setting the layout_gravity to end in the layout. On onOptionsItemSelected (MenuItem item) I replaced the call to the ActionBarDrawerToggle with direct calls to the DrawerLayout.

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // The action bar home/up action should open or close the drawer.
        // ActionBarDrawerToggle will take care of this.
        if (item != null && item.getItemId() == android.R.id.home) {
            if (drawerLayout.isDrawerOpen(Gravity.RIGHT)) {
                drawerLayout.closeDrawer(Gravity.RIGHT);
            } else {
                drawerLayout.openDrawer(Gravity.RIGHT);
            }
            return true;
        }
        /*if (drawerToggle.onOptionsItemSelected(item)) {
            return true;
        }*/


        return false;
    }
    
answered by 26.05.2017 в 19:51