Search code examples
androidkotlinandroid-jetpack-compose

How to give Modal Bottom Sheet Max Min height in Compose


I am using ModalBottomSheet in my Android App, I want to give a minimum, maximum height to this sheet.

    ModalBottomSheet(
        onDismissRequest = {
            // Do not allow dismiss unless the close button has been clicked
            if (previewSheetState.isVisible && isClosable) {
                scope.launch {
                    onBottomSheetDismiss()
                    previewSheetState.hide()
                }
            }
        },
        sheetState = previewSheetState,
        modifier = Modifier
            .fillMaxSize()
         ){
//Content of sheet
}

I want this sheet to open max 90 percent of the screen and min 50%


Solution

  • Set heightIn() in your modifier. To achieve:

    I want this sheet to open max 90 percent of the screen and min 50%

    You can get your screen height by using LocalConfiguration.current:

    val configuration = LocalConfiguration.current
    val minHeight = configuration.screenHeightDp * 0.5f // 50% of screen height
    val maxHeight = configuration.screenHeightDp * 0.9f // 90% of screen height
    

    Then set these values to the min and max parameters of heightIn():

     modifier = Modifier
        .fillMaxWidth()
        .heightIn(min = minHeight.dp, max = maxHeight.dp)
    

    This way, your ModalBottomSheet will have a minimum height of 50% and a maximum height of 90% of the screen height