Ask Your Question
2

How can I detect when someone clicks outside of an Android Compose ModalBottomSheetLayout?

asked 2021-12-02 11:00:00 +0000

scrum gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2022-05-16 07:00:00 +0000

djk gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-12-02 11:00:00 +0000

Seen: 17 times

Last updated: May 16 '22