Short answer: to center content in a Column, use horizontalAlignment = Alignment.CenterHorizontally and verticalArrangement = Arrangement.Center, and make the Column fill the available space with fillMaxSize(). In a Box, use contentAlignment = Alignment.Center.
Center everything in a Column
@Composable
fun CenteredColumn() {
Column(
modifier = Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
Text("Hello")
Text("Centered on both axes")
}
}
Without fillMaxSize(), the Column is only as large as its content, so there is no extra space to center within. This is the most common reason centering seems not to work.
Center in a Row
Row(
modifier = Modifier.fillMaxSize(),
horizontalArrangement = Arrangement.Center,
verticalAlignment = Alignment.CenterVertically
) {
Icon(Icons.Default.Favorite, contentDescription = null)
Text("Favorites")
}
In a Row, the arrangement controls the main (horizontal) axis and the alignment controls the cross (vertical) axis. In a Column it is the other way around.
Center in a Box
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
CircularProgressIndicator()
}
A Box stacks children on top of each other. contentAlignment aligns all of them, and you can align a single child with Modifier.align(...).
Align just one child
Column(Modifier.fillMaxWidth()) {
Text("Left aligned (default)")
Text(
text = "Centered",
modifier = Modifier.align(Alignment.CenterHorizontally)
)
Text(
text = "Right aligned",
modifier = Modifier.align(Alignment.End)
)
}
Spacing options with Arrangement
| Arrangement | Result |
|---|---|
Arrangement.Center |
Items grouped in the middle |
Arrangement.SpaceBetween |
First and last at the edges, equal gaps between |
Arrangement.SpaceEvenly |
Equal gaps everywhere, including edges |
Arrangement.SpaceAround |
Equal space around each item |
Arrangement.spacedBy(8.dp) |
A fixed gap between items |
XML gravity vs Compose
| XML | Compose |
|---|---|
android:gravity (aligns the children) |
horizontalAlignment, verticalArrangement, contentAlignment on the container |
android:layout_gravity (aligns the view in its parent) |
Modifier.align(...) on the child |
Common mistakes
- Forgetting fillMaxSize or fillMaxWidth. The container wraps its content, so nothing looks centered.
- Mixing up arrangement and alignment. In a Column, vertical is arrangement and horizontal is alignment.
- Using Modifier.align on a Row or Column that is not the direct parent.
alignworks only for direct children of that container. - Centering text inside a wide Text. Use
textAlign = TextAlign.Centeron the Text itself when the Text is as wide as its parent.
FAQ
How do I center a Column in Jetpack Compose?
Use Modifier.fillMaxSize() with horizontalAlignment = Alignment.CenterHorizontally and verticalArrangement = Arrangement.Center.
How do I center one item in a Column?
Give that item Modifier.align(Alignment.CenterHorizontally).
What is the Compose version of gravity?
Alignment and arrangement parameters on the container, and Modifier.align for a single child.
How do I center text?
Wrap it in a Box with contentAlignment = Alignment.Center, or use textAlign = TextAlign.Center for multi-line text.
Next: weights in Jetpack Compose for sharing space, and more Compose tutorials.