Open Navigation Drawer with a Handle

This type of functionality is not supported on DrawerLayout from support libraries. There are some third party libraries with all kind of additional functionality. They are huge and usually we don’t need all components included.

The implementation is very easy. After looking at source code of DrawerLayout class, we can see that there is onTouchEvent method predefined. So we have to delegate touch events from our “handle” widget to DrawerLayout.

 drag.setOnTouchListener(new View.OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {

       if(event.getAction() == MotionEvent.ACTION_DOWN){
          MotionEvent copy = MotionEvent.obtainNoHistory(event);
          copy.setEdgeFlags(ViewDragHelper.EDGE_ALL);
          copy.setLocation(0, event.getY());

          drawerLayout.onTouchEvent(copy);
       }
       return false;
   }
});

Object drag could be any type of View as long as we can listen to touch events. If touch action equals ACTION_DOWN than we delegate event handling to DrawerLayout object with motion event position X set to 0. All next updates will bi automatically propagated to DrawerLayout instance.