I have found the solution to collapse BottomSheetBehavior
when clicking outside here. The code from said link is below (converted to Kotlin):
override fun dispatchTouchEvent(event: MotionEvent): Boolean {
var returnValue: Boolean = super.dispatchTouchEvent(event)
if (event.action == MotionEvent.ACTION_DOWN) {
if (mBottomSheetBehavior?.state == BottomSheetBehavior.STATE_EXPANDED) {
val outRect = Rect()
val fragment = supportFragmentManager.findFragmentById(R.id.queueChoicePanel)
fragment?.view?.getGlobalVisibleRect(outRect)
if (!outRect.contains(event.rawX.toInt(), event.rawY.toInt())) {
mBottomSheetBehavior?.state = BottomSheetBehavior.STATE_COLLAPSED
}
}
}
return returnValue
}
However, what it lacks is that when I click outside and I click an area that is clickable, it also fires that clickable area's click handler. I want it so when the BottomSheetBehavior
is in its expanded state (i.e. BottomSheetBehavior.state == BottomSheetBehavior.STATE_EXPANDED
) and I click outside, I collapse the BottomSheetBehavior
and intercepting the click so that it does not further trigger click handlers from outside the BottomSheetBehavior
(for example, clicking a button that is outside BottomSheetBehavior
and disabling the button's click handler from firing). How do I do that?