android – DrawerLayout.openDrawer does not work the first time-ThrowExceptions

Exception or error:

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?

How to solve:

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

###

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);
}

###

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

###

Please use clearFocus() method for DrawerLayout object.

###

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>

###

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.

###

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" />

Leave a Reply

Your email address will not be published. Required fields are marked *