You must handle the touch position in the dispatchTouchEvent() method. Read more about sensory hierarchy here.
@Override public boolean dispatchTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_UP) { if (mDrawerLayout.isDrawerOpen(mRightDrawerListView)) { View content = findViewById(R.id.right_drawer); int[] contentLocation = new int[2]; content.getLocationOnScreen(contentLocation); Rect rect = new Rect(contentLocation[0], contentLocation[1], contentLocation[0] + content.getWidth(), contentLocation[1] + content.getHeight()); View toolbarView = findViewById(R.id.toolbar); int[] toolbarLocation = new int[2]; toolbarView.getLocationOnScreen(toolbarLocation); Rect toolbarViewRect = new Rect(toolbarLocation[0], toolbarLocation[1], toolbarLocation[0] + toolbarView.getWidth(), toolbarLocation[1] + toolbarView.getHeight()); if (!(rect.contains((int) event.getX(), (int) event.getY())) && !toolbarViewRect.contains((int) event.getX(), (int) event.getY())) { isOutSideClicked = true; } else { isOutSideClicked = false; } } else { return super.dispatchTouchEvent(event); } } else if (event.getAction() == MotionEvent.ACTION_DOWN && isOutSideClicked) { isOutSideClicked = false; return super.dispatchTouchEvent(event); } else if (event.getAction() == MotionEvent.ACTION_MOVE && isOutSideClicked) { return super.dispatchTouchEvent(event); } if (isOutSideClicked) {
source share