requiredHeight

Compose Modifier

Common
fun Modifier.requiredHeight(intrinsicSize: IntrinsicSize) =
    this then
        IntrinsicHeightElement(
            height = intrinsicSize,
            enforceIncoming = false,
            inspectorInfo =
                debugInspectorInfo {
                    name = "requiredHeight"
                    properties["intrinsicSize"] = intrinsicSize
                },
        )

Declare the height of the content to be exactly the same as the min or max intrinsic height of the content. The incoming measurement Constraints will not override this value. If the content intrinsic height does not satisfy the incoming Constraints, the parent layout will be reported a size coerced in the Constraints, and the position of the content will be automatically offset to be centered on the space assigned to the child by the parent layout under the assumption that Constraints were respected.

See width for options of sizing to intrinsic width. See height and heightIn for options to set the preferred height. See requiredHeight and requiredHeightIn for other options to set the required height.

Common
fun Modifier.requiredHeight(height: Dp) =
    this.then(
        SizeElement(
            minHeight = height,
            maxHeight = height,
            enforceIncoming = false,
            inspectorInfo =
                debugInspectorInfo {
                    name = "requiredHeight"
                    value = height
                },
        )
    )

Declare the height of the content to be exactly heightdp. The incoming measurement Constraints will not override this value. If the content chooses a size that does not satisfy the incoming Constraints, the parent layout will be reported a size coerced in the Constraints, and the position of the content will be automatically offset to be centered on the space assigned to the child by the parent layout under the assumption that Constraints were respected.

See requiredHeightIn and requiredSizeIn to set a size range. See height to set a preferred height, which is only respected when the incoming constraints allow it.

Code Examples

SimpleRequiredHeightModifier

@Composable
fun SimpleRequiredHeightModifier() {
    // The result is a 50.dp x 50.dp blue box centered in a 100.dp x 100.dp space.
    // Note that although a previous modifier asked it to be 100.dp height, this
    // will not be respected. They would be respected if height was used instead of requiredHeight.
    Box(
        Modifier.requiredHeight(100.dp).requiredHeight(50.dp).aspectRatio(1f).background(Color.Blue)
    )
}