DrawerLayout.openDrawer does not work the first time
Asked Answered
M

8

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?

Misrepresent answered 6/8, 2013 at 19:10 Comment(4)
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?Fillender
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 nothingMisrepresent
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?Fillender
Yes, the method is getting called... :(Misrepresent
M
46

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

Mezcaline answered 16/1, 2014 at 17:39 Comment(1)
visibility set to "gone" + drawer.openDrawer(Rina
B
7

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

Bamby answered 24/1, 2019 at 11:46 Comment(1)
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).Gules
S
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);
}
Smoot answered 6/8, 2013 at 20:27 Comment(1)
It worth noting that "mDrawerToggle.onOptionsItemSelected" return false if one of fragment has an ActionBar menu.Dysgraphia
B
0

Please use clearFocus() method for DrawerLayout object.

Bresee answered 4/2, 2014 at 12:25 Comment(0)
A
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>

Acupuncture answered 6/2, 2014 at 19:48 Comment(0)
B
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.

Butyl answered 18/4, 2017 at 22:55 Comment(0)
N
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" />
Numerical answered 19/9, 2019 at 11:27 Comment(0)
E
0

Select your NavigationView > Set visibility properties to visible

enter image description here

Equipment answered 18/3, 2022 at 9:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.