Introduction
A Spacer composable is used to add empty space between UI elements. It helps to organize the layout and make the app look neat and easy to use.
A Spacer composable is used to add empty space between UI elements. It helps to organize the layout and make the app look neat and easy to use.
Spacer(modifier = Modifier.size(16.dp))Spacer(modifier = Modifier.height(24.dp))Spacer(modifier = Modifier.width(12.dp))Spacer(modifier = Modifier.size(8.dp))This example shows two text elements stacked vertically with a 20dp space between them using Spacer.
import androidx.compose.foundation.layout.* import androidx.compose.material3.* import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.ui.unit.dp @Composable fun SpacerExample() { Column(modifier = Modifier.padding(16.dp)) { Text(text = "Hello") Spacer(modifier = Modifier.height(20.dp)) Text(text = "World") } }
Spacer does not draw anything visible; it only creates empty space.
Use Spacer inside layouts like Row, Column, or Box to control spacing.
Remember to import androidx.compose.foundation.layout.Spacer and Modifier.
Spacer adds empty space between UI elements.
Use Modifier.size(), .width(), or .height() to set space size.
It helps make layouts clean and easy to read.
Spacer composable in Jetpack Compose?Row {
Text("Hello")
Spacer(modifier = Modifier.width(24.dp))
Text("World")
}Column {
Text("Top")
Spacer(Modifier.size(20.dp))
Text("Bottom")
}Spacer(Modifier.size(20.dp)) is syntactically correct.Modifier.size(20.dp) is valid and Spacer works inside Column to occupy vertical space.Pass the test and this pattern moves unknown