Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You can detect when someone clicks outside of an Android Compose ModalBottomSheetLayout by using the setDismissOnClickOutside method.

First, add a reference to your ModalBottomSheetLayout as a state in your composable function:

val sheetState = rememberModalBottomSheetState(initialValue = ModalBottomSheetValue.Hidden)

Next, create your ModalBottomSheetLayout and set the dismissOnClickOutside property to true:

ModalBottomSheetLayout(
    sheetState = sheetState,
    dismissOnClickOutside = true, // Set this property to true
) {
    // Your content goes here
}

Finally, you can listen for changes in the sheetState to determine when the user has clicked outside of the ModalBottomSheetLayout:

sheetState.addOnChangeListener { state ->
    if (state == ModalBottomSheetValue.Hidden) {
        // User clicked outside of the bottom sheet
    }
}

This will trigger the code inside the if statement when the user clicks outside of the bottom sheet.