How to detect a touch event outside the navigation box

My application has an Android Navigation box. I can open / close the drawer when the user touches the side of the navigation drawer. Can any of you help me detect a touch / click event when the user touches / clicks on the sides of the navigation box. I need to perform some functions in this case. Please check the attached screenshot. enter image description here Any help would be assigned.

+6
source share
3 answers

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) { //make http call/db request Toast.makeText(this, "Hello..", Toast.LENGTH_SHORT).show(); } return super.dispatchTouchEvent(event); } 
+9
source

You can use onDrawerClosed

when touching an external screen, onDrawerClosed is called after the Drawer navigation is closed

 public void onDrawerClosed(View view) { super.onDrawerClosed(view); //do here } 
0
source

You can spend some time checking this out.

DrawerLayout.DrawerListener

I hope this helps you, how it helps me.

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.

0
source

Source: https://habr.com/ru/post/1238229/


All Articles