24

I am using a android.support.v4.widget.DrawerLayout in my activity. As long as I use the swipe gesture to open it, it works fine.

However, when I want to open it through a button click by calling drawer.openDrawer(Gravity.LEFT), it does not work.

But, if I open it once using the swipe gesture, after that it works normally with button click.

Any idea how I could solve or work around this?

Mahm00d
  • 3,881
  • 8
  • 44
  • 83
user2527666
  • 433
  • 5
  • 14
  • Is the button click for just some random button you have, or do you want clicking the icon in the top left to toggle the drawer? – btse Aug 06 '13 at 19:25
  • It's for a random button I have. Again, the strange thing is that it works normally after you open and close it once with the Swipe gesture, while if you try the button first it just does nothing – user2527666 Aug 06 '13 at 23:07
  • That is very odd. Have you put in a Log statement to make sure that the method that your button calls is called on the first click? – btse Aug 07 '13 at 16:54
  • Yes, the method is getting called... :( – user2527666 Aug 08 '13 at 01:13

8 Answers8

45

I had the same issue and I've just found out that for some reason the FrameLayout that represents the drawer have the visibility set to "gone", that probably goes to "visible" during the first slideGesture.

So, open your layout xml file, locate your FrameLayout that represents the drawer and simply erase the visibility setting. My opening tag is now as follows:

<FrameLayout
    android:layout_width="305dp"
    android:layout_height="match_parent"
    android:layout_gravity="start">

That worked for me.

Cheers

Eduardo Lino
  • 791
  • 7
  • 9
6

In my case the visibility on 'NavigationView' was set to gone in the layout. Changing it to visible solved the issue

CodesmithX
  • 246
  • 4
  • 8
  • This is the only solution that was working to me. In your Java/Kotlin code call setVisibility() to View.VISIBLE on the navigation view (Frament in my case, so the parent was FrameLayout). – Borislav Kamenov Apr 07 '20 at 22:29
3

If you want to open it from the Top Left Toggle you should implement onOptionsItemSelected(MenuItem item)

@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 (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

    switch (item.getItemId()) {
    case android.R.id.home:
        return true;
    }
   return super.onOptionsItemSelected(item);
}
Luis Lavieri
  • 4,064
  • 6
  • 39
  • 69
0

Please use clearFocus() method for DrawerLayout object.

Andriy
  • 1
0

Encountered the same problem and was able to fix it by specifying a width for the drawer content element inside the layout.

Without layout_width attribute the drawer did not open on the first openDrawer() call, with the value it does.

<FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</FrameLayout>

<FrameLayout
    android:layout_width="300dp" 
    android:layout_height="match_parent"
    android:layout_gravity="right"
    android:background="#ffffff"
    android:id="@+id/drawer_content">
</FrameLayout>

Anti Veeranna
  • 11,485
  • 4
  • 42
  • 63
0

Too stupid, i did set an empty layout, problem was just as described above, when the drawer was manually dragged the first time, after that, the button worked, but without dragging it first, the navigation drawer did never open.

Don't set an empty layout.

Removing my EmptyLayout from the ListView which represented the NavigationDrawerContent made it work perfect again.

DONT

drawerContentListView.setEmptyView(getLayoutInflater().inflate(R.layout.navigation_drawer_empty_layout, null));

**** costed me more than an hour.

cV2
  • 5,229
  • 3
  • 43
  • 53
0

You can put this code inside your DrawerLayout:

<android.support.design.widget.NavigationView
 android:id="@+id/nav_view"
 android:layout_width="wrap_content"
 android:layout_height="match_parent"
 android:layout_gravity="start"
 app:headerLayout="@layout/nav_header_main"
 app:menu="@menu/activity_main_drawer" />
Giulio Caccin
  • 2,962
  • 6
  • 36
  • 57
0

Select your NavigationView > Set visibility properties to visible

enter image description here

XpressGeek
  • 2,889
  • 3
  • 23
  • 27