22

Jetpack compose has a padding modifier that is actually analogous to the margin attribute in other UI toolkits (it adds space around the target). Is there a way to add inner padding to a component in compose without wrapping it with a Box/Surface?

2 Answers 2

39

Modifier.padding() in Jetpack Compose acts as padding or margin depending on order.

Modifier.padding(10.dp).size(200.dp) adds space before setting size you have a Composable with 200.dp size

Modifier.size(200.dp).padding(10.dp) adds padding which you have 180.dp width and height after setting 10.dp padding on each side.

You can try this with Modifier.background, Modifier.border, Modifier.clickble{} to see how order changes area with color, border or which user can click.

You can also refer codelab's documentation about Modifiers to see how to apply padding.

1
  • Wow that's a pathetic API design from Jetpack Compose developers Commented May 28 at 13:06
1

if you want to padding inside you can set padding to child element

        Row(
        modifier= Modifier
              .fillMaxWidth()
              .border(
                0.dp,
                shape = MaterialTheme.shapes.medium,
                color=Color.White
            )
            .clip(shape = MaterialTheme.shapes.medium)
            .clickable {
                setShowColorPicker(!showColorPicker)
            },
        verticalAlignment = Alignment.CenterVertically
        ) {

        Icon(
            painter = 
            painterResource(id=R.drawable.round_square_24),
            contentDescription = "Color",
            modifier = Modifier.size(24.dp),
            tint = hexToComposeColor(colorHex)
        )

        Text(text = colorHex, color = hexToComposeColor(colorHex),
            modifier = Modifier.padding(vertical = 16.dp),)

Not the answer you're looking for? Browse other questions tagged or ask your own question.