72

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?

Tyler A.
  • 3,048
  • 1
  • 23
  • 27
Rodrigo Amaro Reveco
  • 4,932
  • 5
  • 22
  • 33

6 Answers6

177

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.

Thuy Trinh
  • 2,914
  • 1
  • 22
  • 35
  • 23
    For unlocking it u use mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED); – support_ms Mar 17 '15 at 05:06
  • 8
    This is not working in my case, I always get a drawer on swipe gesture even if I set the Drawer Layout with the above Solution. – Salman Khakwani Aug 17 '15 at 18:27
  • I am having that same issue. – Reid Mac Nov 06 '15 at 15:36
  • @SalmanKhakwani and reid-mac check out Vit-Vetal's answer below. – Fatima Feb 24 '18 at 11:17
  • It also disabled the close gesture and the back button close action. – Dan Meng Jul 27 '18 at 02:50
  • 1
    think LOCK_MODE_LOCKED_CLOSED worked in Compat 24.x, but the functionality has been changed in newer compat libraries and LOCK_MODE_LOCKED_CLOSED now completely prevents the nav menu from showing, even via using the hamburger menu. Other solution is now needed. – Martin Vysny Sep 04 '18 at 06:40
  • Currently there is a bug with the method. I have reported it here: https://issuetracker.google.com/issues/136738274 – i_tanova Jul 04 '19 at 14:41
23

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;
Gopal Singh Sirvi
  • 4,539
  • 5
  • 33
  • 55
greg7gkb
  • 4,933
  • 4
  • 41
  • 55
3

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

Hadi
  • 705
  • 8
  • 23
3

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.

ViT-Vetal-
  • 2,431
  • 3
  • 19
  • 35
  • 1
    You are right. The reason is described well here by @A--C https://stackoverflow.com/questions/18413116/why-i-cant-lock-drawerlayout-with-layout-gravity/18413276#18413276 – Fatima Feb 24 '18 at 11:16
3

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

Martin Vysny
  • 3,088
  • 28
  • 39
2

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.