We just launched Compose Examples featuring over 150+ components! Check it out →

size

Common

Modifier in Compose Foundation Layout

Declare the preferred size of the content to be exactly [size]dp square. The incoming measurement [Constraints] may override this value, forcing the content to be either smaller or larger.

For a modifier that sets the size of the content regardless of the incoming constraints, see [Modifier.requiredSize]. See [width] or [height] to set width or height alone. See [widthIn], [heightIn] or [sizeIn] to set a preferred size range.

Last updated:

Installation

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

Overloads

@Stable
fun Modifier.size(size: Dp)
@Stable
fun Modifier.size(width: Dp, height: Dp)
@Stable
fun Modifier.size(size: DpSize)

Code Examples

SimpleSizeModifier

@Composable
fun SimpleSizeModifier() {
    Box { Box(Modifier.size(100.dp, 100.dp).background(Color.Red)) }
}

SimpleSizeModifierWithDpSize

@Composable
fun SimpleSizeModifierWithDpSize() {
    Box { Box(Modifier.size(DpSize(100.dp, 100.dp)).background(Color.Red)) }
}
by @alexstyl