Sobes.tech
Junior — Middle

How can you set outer margins for a component in Jetpack Compose?

sobes.tech AI

Answer from AI

In Jetpack Compose, to set outside margins of a component, the Modifier.padding() modifier is used. This modifier adds internal padding, but if you need external margins, they are implemented in Compose through Modifier.padding() on the parent element or using a Spacer.

Example of setting margins around a component:

Box(
    modifier = Modifier
        .padding(16.dp) // outside margin of the Box
        .background(Color.Gray)
) {
    Text("Sample text")
}

If you need to separate a component from neighboring elements, you can use a Spacer or wrap the component in a container with the desired margins.