Skip to content Skip to sidebar Skip to footer

Open Navigationview On Right Swipe From Everywhere On The Layout

I would like to open the navigationview on a right swipe, no matter where the right swipe is. Default you have to make the right swipe quite at the side, but I want to swipe for ex

Solution 1:

you could either increase the swipe edge of the drawer using something like this

publicstaticvoidincreaseSwipeEdgeOfDrawer(DrawerLayout mDlSearchDrawer) {
        try {

            FieldmDragger= mDlSearchDrawer.getClass().getDeclaredField(
                    "mRightDragger");//mRightDragger or mLeftDragger based on Drawer Gravity
            mDragger.setAccessible(true);
            ViewDragHelperdraggerObj= (ViewDragHelper) mDragger
                    .get(mDlSearchDrawer);

            FieldmEdgeSize= draggerObj.getClass().getDeclaredField(
                    "mEdgeSize");
            mEdgeSize.setAccessible(true);
            intedge= mEdgeSize.getInt(draggerObj);


            mEdgeSize.setInt(draggerObj, <size of the edge here>);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

or you could give keep a view on top that matches the size of the drawer layout, assign on on tough listener to it to detect a swipe and call DrawerLayout#openDrawer(android.view.View) on it

Solution 2:

You need to @override onTouchEvent(MotionEvent event) and can detect right swipe in your drawer activity, then open drawer in that.

@Override
public boolean onTouchEvent(MotionEvent event)
{     
    switch(event.getAction())
    {
      case MotionEvent.ACTION_DOWN:
          x1 = event.getX();                         
      break;         
      case MotionEvent.ACTION_UP:
          x2 = event.getX();
          float deltaX = x2 - x1;

          if (Math.abs(deltaX) > MIN_DISTANCE)
          {
              // Left to Right swipe action
              if (x2 > x1)
              {
                  Toast.makeText(this, "Left to Right swipe [Next]", Toast.LENGTH_SHORT).show ();   
                  drawer.openDrawer();     //OPEN YOUR DRAWER HERE              
              }

          }
          else
          {
              // consider as something else - a screen tap for example
          }                          
      break;   
    }           
    return super.onTouchEvent(event);       
}

Post a Comment for "Open Navigationview On Right Swipe From Everywhere On The Layout"