How can I make part of the BottomSheet UI not dragable?

The bottom sheet UI is by default dragable.That’s good, I want it to be dragable. But at some specific part of the bottom sheet, I want to disable dragging.

I’m using the bottom sheet to show a receipt to the user. Then the user will have to sign the receipt. There’s a view in the UI that can accept signature. But since the bottom sheet is dragable, signing the receipt shakes the position of the sheet. Is there a way that I can prevent dragging when the user’s touch is inside the signature control?

Answer

Don’t blame me if it doesn’t work, but in the view of time, I have a rough idea to start with. 😉 Just create an onClickListener / onTouchListener to the signature. If the customer clicks or touches it, the dragging functionality of the Bottomsheet will be disabled with something like that inside the above mentioned Listener:

final BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
        behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState) {
                if (newState == BottomSheetBehavior.STATE_DRAGGING) {
                    behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
                }
            }

            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {
            }
        });

Don’t forget to enable the dragging function after the signature isn’t clicked or touched anymore. Cheers.

@Lance: This one worked for me: Subscribing to the setOnTouchListener and then changing the isDraggable on touch up/down.

signatureCaptureControl.setOnTouchListener { v, event ->
  when (event.action) {
    MotionEvent.ACTION_UP -> {
      bottomSheetBehavior.isDraggable = true
      v.performClick()
    }
    MotionEvent.ACTION_DOWN -> bottomSheetBehavior.isDraggable = false
  }
}