I have implemented Android Navigation drawer in my application. I am able to open/close the drawer when user touches the out side of navigation drawer. Can any one of you help me in detect the touch/click event when user touch/click out side the navigation drawer. I need to perform some functionality in that event.
Please check the attached screenshot.
Any help would be appriciated.
Asked
Active
Viewed 5,303 times
5

Ganesh
- 662
- 3
- 8
- 26
-
same issue in my case also found any solution for this, b'coz in my case below acceptable answer is not working. – Gaurav Mandlik Mar 11 '22 at 06:18
3 Answers
8
You have to handle the touch position in dispatchTouchEvent()
method. Check more about touch 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) {
//make http call/db request
Toast.makeText(this, "Hello..", Toast.LENGTH_SHORT).show();
}
return super.dispatchTouchEvent(event);
}

Community
- 1
- 1

Noundla Sandeep
- 3,334
- 5
- 29
- 56
1
you may spend some time by checking this out.
I hope it will help you as it helps me.
An example:
DrawerLayout drawerLayout = activity.findViewById(R.id.drawer_layout);
drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(@NonNull View view, float v) {
}
@Override
public void onDrawerOpened(@NonNull View view) {
}
@Override
public void onDrawerClosed(@NonNull View view) {
}
@Override
public void onDrawerStateChanged(int i) {
}
});
Good luck.

Iris Tako
- 133
- 10
0
You can use onDrawerClosed
when you touch outside screen
onDrawerClosed
will call after close of Navigation Drawer
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
//do here
}

Kishore Jethava
- 6,666
- 5
- 35
- 51
-
1Thanks for your answer. onDrawerClosed(View view) will call in many scenarios. that will not work for me. – Ganesh Dec 15 '15 at 08:10
-
may be help [this](http://stackoverflow.com/questions/22590247/android-navigation-drawer-doesnt-pass-ontouchevent-to-activity) – Kishore Jethava Dec 15 '15 at 08:15