disable the swipe gesture that opens the navigation drawer in android

92,992

Solution 1

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.

Solution 2

for locking you can do this:

mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

and for unlock :

mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);

Solution 3

Add gravity value too when using setDrawerLockMode();

Do this :

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

This should work like a charm

Solution 4

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

Solution 5

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

Share:
92,992

Related videos on Youtube

user1627990
Author by

user1627990

Updated on July 28, 2021

Comments

  • user1627990
    user1627990 almost 3 years

    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?

  • thanhnh
    thanhnh almost 11 years
    Are you sure? I tried but it was diabled open and close, both. I were testing on android 2.3x with ActionBarSherlock framework.
  • Tran Hieu
    Tran Hieu over 10 years
    I'm sure because I've used it in my code. But I did not use with ActionBarSherlock. I used with ActionBar only.
  • egfconnor
    egfconnor over 10 years
    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.
  • Igoussam
    Igoussam about 10 years
    I have corrected the answer to LOCK_MODE_LOCKED_CLOSED.
  • Steve Liddle
    Steve Liddle almost 10 years
    If you don't already have mDrawerLayout, set it as follows: mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
  • Azurespot
    Azurespot about 9 years
    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.
  • Dylan Vander Berg
    Dylan Vander Berg over 8 years
    Try overriding onDrawerSlide() in your DrawerListener and not calling the super method.
  • Mattia Ruggiero
    Mattia Ruggiero over 7 years
    Thanks for clarifying the accepted answer with the LOCK_MODE_UNLOCKED flag
  • TheWhiteLlama
    TheWhiteLlama about 7 years
    I think this is better than the accepted answer since it also introduces the opposing method
  • Timo Albert
    Timo Albert over 6 years
    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)
  • Sanjeev Sangral
    Sanjeev Sangral about 6 years
    mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKE‌​D_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.
  • HelloWorld
    HelloWorld about 6 years
    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
  • Martin Vysny
    Martin Vysny over 5 years
    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
    Martin Vysny over 5 years
    This is the correct answer. Let me elaborate on this a bit more in the answer.
  • ProjectJourneyman
    ProjectJourneyman over 5 years
    In the current sources, it looks like 3 for left and 5 for right will work as expected, but GravityCompat.END does not.
  • Syed Hussain Mehdi
    Syed Hussain Mehdi about 5 years
    If I used mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKE‌​D_CLOSED); for locking it closed the drawer so, for locking i used mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKE‌​D_OPEN);
  • i_tanova
    i_tanova almost 5 years
    Currently there is a bug with the method. I have reported it here: issuetracker.google.com/issues/136738274
  • Michael Tolsma
    Michael Tolsma over 3 years
    error: cannot find symbol mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKE‌​D_CLOSED);