202

I've been following the navigation drawer guide by Google and I'd like to add it to an Activity with tabs and gestures.

I'd like to disable the gesture to open the navigation drawer, does anyone have any idea how to do this?

Alexander Farber
  • 18,345
  • 68
  • 208
  • 375
user1627990
  • 2,437
  • 2
  • 13
  • 18

6 Answers6

472

You should use:

mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

It worked for me, the swipe to open the drawer was disabled.

If it still won't work, check out the answer provided here.

NPovlsen
  • 154
  • 14
Tran Hieu
  • 5,047
  • 1
  • 10
  • 10
  • 1
    Are you sure? I tried but it was diabled open and close, both. I were testing on android 2.3x with ActionBarSherlock framework. – thanhnh Jul 26 '13 at 04:46
  • I'm sure because I've used it in my code. But I did not use with ActionBarSherlock. I used with ActionBar only. – Tran Hieu Aug 20 '13 at 10:00
  • 109
    To clarify: DrawerLayout.LOCK_MODE_LOCKED_OPEN locks the drawer to the open state so the user can't hide it. DrawerLayout.LOCK_MODE_LOCKED_CLOSED locks the drawer to the closed state so the user can't open it. Lastly, DrawerLayout.LOCK_MODE_UNLOCKED unlocks the drawer so it can be open or closed. – egfconnor Oct 23 '13 at 15:47
  • I have corrected the answer to LOCK_MODE_LOCKED_CLOSED. – Intrications Feb 18 '14 at 14:51
  • 4
    If you don't already have `mDrawerLayout`, set it as follows: `mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);` – Steve Liddle Jul 23 '14 at 17:01
  • 6
    But how about if we want to control the open/close with the app icon only, and not swiping? Is there a way to do that? I am curious because I'm trying to implement the new `ToolBar` (API 21) and use the `SlidingTabLayout` feature it has, which is conflicting with my Nav. drawer swiping. So I'd rather disable the Nav. drawer slide in favor of the `ToolBar` sliding feature. – Azurespot Feb 11 '15 at 02:42
  • Try overriding `onDrawerSlide()` in your `DrawerListener` and not calling the super method. – Dylan Vander Berg Aug 11 '15 at 14:50
  • Also if you have two drawers on both sides you can lock one by getting the view of the NavigationView and doing `drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, navigationView)` – Timo Sep 05 '17 at 11:46
  • 3
    this disables the entire drawer (e.g. icon does not work anymore), not just swiping, hence did not answer the question -- which specifically referred to swiping – HelloWorld Mar 04 '18 at 12:37
  • 1
    I 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:37
  • 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:43
116

for locking you can do this:

mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

and for unlock :

mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
saleh sereshki
  • 2,041
  • 3
  • 14
  • 18
14

Add gravity value too when using setDrawerLockMode();

Do this :

drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, GravityCompat.END);

This should work like a charm

Jorgesys
  • 114,263
  • 22
  • 306
  • 247
Burhan Shakir
  • 217
  • 3
  • 10
  • 2
    mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); lock the both Drawer and drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, GravityCompat.END); represent which Drawer you want to lock thanks man it works for me and save my time. – Sanjeev Sangral Feb 10 '18 at 05:19
  • In the current sources, it looks like 3 for left and 5 for right will work as expected, but GravityCompat.END does not. – ProjectJourneyman Nov 15 '18 at 00:23
7

To disable swiping, override onInterceptTouchEvent and onTouchEvent on DrawerLayout and have them return false.

HelloWorld
  • 2,271
  • 21
  • 34
7

The answer to disable swiping is the correct one. I 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.

The following class works for me (Kotlin):

class MyDrawerLayout(ctx: Context) : DrawerLayout(ctx) {
  var isSwipeOpenEnabled: Boolean = true

  override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
      if (!isSwipeOpenEnabled && !isDrawerVisible(Gravity.START)) {
          return false
      }
      return super.onInterceptTouchEvent(ev)
  }

  @SuppressLint("ClickableViewAccessibility")
  override fun onTouchEvent(ev: MotionEvent): Boolean {
      if (!isSwipeOpenEnabled && !isDrawerVisible(Gravity.START)) {
          return false
      }
      return super.onTouchEvent(ev)
  }
}
Martin Vysny
  • 2,336
  • 25
  • 29
0

This works for me

mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, {Your drawer view});
user350524
  • 39
  • 4