Build apps faster with our new App builder! Check it out →

wrapContentHeight

Common

Modifier in Compose Foundation Layout

Allow the content to measure at its desired height without regard for the incoming measurement [minimum height constraint][Constraints.minHeight], and, if [unbounded] is true, also without regard for the incoming measurement [maximum height constraint][Constraints.maxHeight]. If the content's measured size is smaller than the minimum height constraint, [align] it within that minimum height space. If the content's measured size is larger than the maximum height constraint (only possible when [unbounded] is true), [align] over the maximum height space.

Last updated:

Installation

dependencies {
   implementation("androidx.compose.foundation:foundation-layout:1.8.0-alpha04")
}

Overloads

@Stable
fun Modifier.wrapContentHeight(
    align: Alignment.Vertical = Alignment.CenterVertically,
    unbounded: Boolean = false
)

Code Example

SimpleWrapContentVerticallyAlignedModifier

@Composable
fun SimpleWrapContentVerticallyAlignedModifier() {
    // Here the result will be a 50.dp x 20.dp blue box centered vertically in a 50.dp x 50.dp
    // space. Because of the size modifier, if wrapContentHeight did not exist,
    // the blue rectangle would actually be 50.dp x 50.dp to satisfy the size set by the modifier.
    // However, because we provide wrapContentHeight, the blue rectangle is specified to be wrap
    // content in height - if the desired height is smaller than 50.dp, it will be centered
    // vertically in this space. Therefore the 50.dp x 20.dp is centered vertically in the space.
    Box(
        Modifier.size(50.dp)
            .wrapContentHeight(Alignment.CenterVertically)
            .height(20.dp)
            .background(Color.Blue)
    )
}
by @alexstyl