Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android BottomSheet how to collapse when clicked outside?

I have implemented bottomsheet behavior with NestedScrollView. And was wondering if it is possible to hide the bottomsheet view when touched outside.

like image 651
Pradeep Kumar Kushwaha Avatar asked Jul 04 '16 13:07

Pradeep Kumar Kushwaha


1 Answers

Finally I was able to do this,

Used the following lines of code:

@Override public boolean dispatchTouchEvent(MotionEvent event){     if (event.getAction() == MotionEvent.ACTION_DOWN) {         if (mBottomSheetBehavior.getState()==BottomSheetBehavior.STATE_EXPANDED) {              Rect outRect = new Rect();             bottomSheet.getGlobalVisibleRect(outRect);              if(!outRect.contains((int)event.getRawX(), (int)event.getRawY()))                 mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);         }     }      return super.dispatchTouchEvent(event); } 

Hope it save someone's whole day!

like image 198
Pradeep Kumar Kushwaha Avatar answered Sep 19 '22 18:09

Pradeep Kumar Kushwaha