0

I'm using Bottom App Bar in my app as a navigational component. When an item of Navigation Drawer (appearing from bottom) is clicked, Navigation Drawer is supposed to be closed, but it is not. When navigation item of Navigation Drawer is clicked, the related fragment is opened, but Navigation Drawer (of Bottom App Bar) still appears.

I have tried to use DrawerLayout and drawerLayout.closeDrawer(Gravity.LEFT) but it does not work.

Here is the layout:

    <?xml version="1.0" encoding="utf-8"?>

    <RelativeLayout
         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/drawerView"
         android:layout_width="match_parent"
         android:layout_height="match_parent">

           <androidx.drawerlayout.widget.DrawerLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:id="@+id/drawerLayout"
              tools:openDrawer="left">

                  <RelativeLayout
                          android:layout_width="match_parent"
                          android:layout_height="match_parent">


                  <com.google.android.material.navigation.NavigationView
                         android:id="@+id/navigation_view"
                         android:layout_width="match_parent"
                         android:layout_height="match_parent"
                         app:headerLayout="@layout/nav_header_layout"
                         app:menu="@menu/bottom_nav_drawer_menu" >

        </com.google.android.material.navigation.NavigationView>
        </RelativeLayout>
   </androidx.drawerlayout.widget.DrawerLayout>
</RelativeLayout>

Here is the code part at which IDE gives error:

        navigation_view.setNavigationItemSelectedListener {

        drawerLayout.closeDrawer(Gravity.LEFT)

        ...

        return@setNavigationItemSelectedListener true

    }

The error below occurs:

java.lang.IllegalArgumentException: No drawer view found with gravity LEFT

I have tried the solutions at the link below, but they did not work:

Android DrawerLayout - No drawer view found with gravity

  • I really don't understand what your exact setup is, but the drawer in a `` must be a direct child of it. Move the `` out of the inner ``, and add the `layout_gravity`, as shown in kandroidj's answer. Also, the outermost `` is pointless, and can be removed. – Mike M. Aug 08 '19 at 11:59

2 Answers2

1

You need to specify the gravity:

<com.google.android.material.navigation.NavigationView
      android:id="@+id/navigation_view"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_gravity="left"
      app:headerLayout="@layout/nav_header_layout"
      app:menu="@menu/bottom_nav_drawer_menu" >

Also, consider using:

drawerLayout.closeDrawer(GravityCompat.LEFT)
kandroidj
  • 13,784
  • 5
  • 64
  • 76
  • Still gives the same error.. By the way, in order to add android:layout_gravity, I have changed inner RelativeLayout to FrameLayout, but it did not work. – Cansu Yeksan Aug 07 '19 at 20:13
  • I think I have to use slideDown() method of HideBottomViewOnScrollBehavior class, but the method is protected, so I cannot use it. – Cansu Yeksan Aug 07 '19 at 20:21
0

I have solved my problem by dismiss() function instead of closeDrawer():

    navigation_view.setNavigationItemSelectedListener {

    ... 

    dismiss()

    ...

    return@setNavigationItemSelectedListener true

}
  • `dismiss()`? Is this in a `BottomSheetDialog`? If so, you don't want the `` at all. That's for drawers that slide in from the left and right sides. In fact, if that is the case, you don't need most of the layout you've shown in the question. All you need is the ``, and you can remove everything else. – Mike M. Aug 08 '19 at 12:10
  • 1
    You are right. Yes, this is in a BottomSheetDialogFragment. I had added in order to close it. Since dismiss() can handle it, I have removed the everything except . Thank you! – Cansu Yeksan Aug 08 '19 at 13:31