Disable gesture listener on DrawerLayout

AndroidDrawerlayout

Android Problem Overview


How can I disable the gesture recognition for the DrawerLayout? (swipe left to right) and only accept the close gesture (right to left) and open the drawer just with the home button?

Android Solutions


Solution 1 - Android

This worked for me:

mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

You can expand the drawer by tapping the Home button, and can use right-to-left swipe gesture to dismiss it. However, left-to-right swipe is no longer triggered.

Solution 2 - Android

For setDrawerLockMode(), this is in the code but not on the Android developer docs:

/**
 * The drawer is unlocked.
 */
public static final int LOCK_MODE_UNLOCKED = 0;

/**
 * The drawer is locked closed. The user may not open it, though
 * the app may open it programmatically.
 */
public static final int LOCK_MODE_LOCKED_CLOSED = 1;

/**
 * The drawer is locked open. The user may not close it, though the app
 * may close it programmatically.
 */
public static final int LOCK_MODE_LOCKED_OPEN = 2;

Solution 3 - Android

To disable the DrawerLayout gesture recognition use:

mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);

Then, to enable right to left swipe, check this resource: http://android-journey.blogspot.com/2010/01/android-gestures.html

Solution 4 - Android

Look like I found bug. For example if set:

android:layout_gravity="right"

or

android:layout_gravity="left"

for drawer content and use .setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED) all will be fine.

But in case when android:layout_gravity="left|center_vertical" or something like it LOCK_MODE_LOCKED_CLOSED will not work.

Solution 5 - Android

The LOCK_MODE_LOCKED_CLOSED nowadays completely prevents the nav menu from showing, even via the hamburger menu (which may be unwanted). The following worked for me: https://stackoverflow.com/a/52160351/377320

Solution 6 - Android

This worked for me:

mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

You can expand the drawer by tapping the Hamburger icon or button from which you have to trigger. However, left-to-right swipe is no longer triggered.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRodrigo Amaro RevecoView Question on Stackoverflow
Solution 1 - AndroidThuy TrinhView Answer on Stackoverflow
Solution 2 - Androidgreg7gkbView Answer on Stackoverflow
Solution 3 - AndroidHadiView Answer on Stackoverflow
Solution 4 - AndroidViT-Vetal-View Answer on Stackoverflow
Solution 5 - AndroidMartin VysnyView Answer on Stackoverflow
Solution 6 - AndroidPrakhar GuptaView Answer on Stackoverflow