1

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?

Lance
  • 2,774
  • 4
  • 37
  • 57
  • Another possible solution: Modify this solution little bit to suit your needs https://stackoverflow.com/a/73903301/3575423 – prodev Sep 30 '22 at 02:13

1 Answers1

1

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
  }
}
Lance
  • 2,774
  • 4
  • 37
  • 57
Ole Pannier
  • 3,208
  • 9
  • 22
  • 33
  • Thanks @DEX7RA. This gave me an idea on how to approach the problem. I was able to try each event and found out that I can setOnTouchListener of the signature View. When It gets touch, the isDraggable is being set to false otherwise, it's set to true. I'll edit you answer and put in my code. – Lance Jun 27 '21 at 18:40
  • Glad to hear that. Collaboration as it should be! :) – Ole Pannier Jun 27 '21 at 18:55