Question
How to Center Items in a Jetpack Compose Column
Question
In Jetpack Compose, I am creating a layout with a Column and I want the items inside that column to be centered.
Column(modifier = ExpandedWidth) {
Text(text = item.title)
Text(text = item.description)
}
How can I center the Text items inside the Column?
Short Answer
By the end of this page, you will understand how alignment works in a Jetpack Compose Column, how to center children horizontally or vertically, and which parameters to use for common layout cases.
Concept
In Jetpack Compose, a Column places its children vertically, one below another. Because of that, there are two different directions to think about:
- Main axis: vertical direction
- Cross axis: horizontal direction
This matters because centering depends on which axis you want to center on.
For a Column:
- Use
horizontalAlignmentto align children left, center, or right horizontally. - Use
verticalArrangementto control how children are placed top, center, bottom, or spaced vertically.
If you want text items in a Column to appear centered like a typical "gravity center" layout, you often need one or both of these:
horizontalAlignment = Alignment.CenterHorizontally
verticalArrangement = Arrangement.Center
A very important detail is that vertical centering only works if the Column has extra height available. If the Column is only as tall as its children, there is no empty space to center within.
That is why developers often combine centering with modifiers like:
Modifier.fillMaxWidth()
Modifier.fillMaxSize()
Understanding axis-based alignment is essential in Compose because layout code is built around these rules. Once you understand them, you can control placement precisely in , , and .
Mental Model
Think of a Column like a stack of papers placed from top to bottom on a table.
horizontalAlignmentdecides whether each paper is placed on the left, center, or right of the table.verticalArrangementdecides whether the whole stack starts at the top, sits in the middle, or stays at the bottom.
So if your Text elements are stacked in a Column:
- Want each line centered left-to-right? Use
horizontalAlignment. - Want the whole group centered top-to-bottom? Use
verticalArrangement. - Want both? Use both settings together.
This is similar to the old idea of "gravity," but Compose expresses it more explicitly through alignment and arrangement.
Syntax and Examples
The core syntax for centering items in a Column is:
Column(
modifier = Modifier.fillMaxWidth(),
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(text = item.title)
Text(text = item.description)
}
This centers the children horizontally inside the Column.
Center horizontally
Column(
modifier = Modifier.fillMaxWidth(),
horizontalAlignment = Alignment.CenterHorizontally
) {
Text("Title")
Text("Description")
}
Explanation:
fillMaxWidth()gives theColumnthe full available width.horizontalAlignment = Alignment.CenterHorizontallycenters each child in that width.
Center vertically and horizontally
Column(
modifier = Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
Text("Title")
Text("Description")
}
Explanation:
fillMaxSize()gives theColumnfull width and height.
Step by Step Execution
Consider this example:
Column(
modifier = Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
Text("Hello")
Text("World")
}
Here is what happens step by step:
Modifier.fillMaxSize()makes theColumntake the full available screen space.- The
Columndecides to place children vertically, because that is what aColumndoes. verticalArrangement = Arrangement.Centertells theColumnto place the whole group of children in the vertical middle of its available height.horizontalAlignment = Alignment.CenterHorizontallytells theColumnto place each child in the horizontal middle of its available width.Text("Hello")is measured and positioned.Text("World")is measured and positioned belowHello.- The two
Textitems appear as a vertically stacked group centered on the screen.
If you remove fillMaxSize(), vertical centering may no longer be visible, because the might shrink to fit its contents.
Real World Use Cases
Centering in a Column is common in many Android screens:
- Empty states
- Example: "No messages yet" with a subtitle centered in the middle of the screen.
- Loading screens
- A spinner and message like "Loading data..." centered vertically and horizontally.
- Login or welcome screens
- Title and description centered for a clean visual layout.
- Error screens
- A message and retry hint shown in the middle of the page.
- Cards and dialogs
- Text inside a narrow column centered for readability.
Example empty state:
Column(
modifier = Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
Text("No items found")
Text("Try changing your filters")
}
Real Codebase Usage
In real projects, developers usually combine Column alignment with modifiers and conditional UI patterns.
Common patterns
- Screen-level centering
- Use
fillMaxSize()with bothhorizontalAlignmentandverticalArrangement.
- Use
- Section-level centering
- Use
fillMaxWidth()withhorizontalAlignmentonly.
- Use
- Child-specific alignment
- Use
Modifier.align(Alignment.CenterHorizontally)for one item.
- Use
- Guarded UI states
- Show a centered
Columnfor loading, error, or empty states.
- Show a centered
Example with UI state:
if (items.isEmpty()) {
Column(
modifier = Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
Text("No data available")
Text("Pull to refresh")
}
}
Why this matters in real code
Compose layouts are often nested. A Column may be inside a , , or another layout. In those cases, centering only works as expected if the parent allows enough space.
Common Mistakes
Here are common beginner mistakes when centering items in a Column.
1. Forgetting to give the Column enough width
Broken example:
Column(
horizontalAlignment = Alignment.CenterHorizontally
) {
Text("Title")
Text("Description")
}
Problem:
- The
Columnmay only be as wide as its content. - There is no extra horizontal space to center within.
Fix:
Column(
modifier = Modifier.fillMaxWidth(),
horizontalAlignment = Alignment.CenterHorizontally
) {
Text("Title")
Text("Description")
}
2. Expecting vertical centering without height
Broken example:
Column(
verticalArrangement = Arrangement.Center
) {
Text("Title")
Text("Description")
}
Problem:
- If the
Columnwraps its content height, there is no extra vertical space.
Fix:
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center
) {
Text()
Text()
}
Comparisons
Here is how the main alignment options compare in a Column.
| Technique | What it affects | When to use it | Example |
|---|---|---|---|
horizontalAlignment | All children horizontally | When every child should share the same horizontal alignment | horizontalAlignment = Alignment.CenterHorizontally |
verticalArrangement | Whole child group vertically | When you want children centered, spaced, or placed at top/bottom | verticalArrangement = Arrangement.Center |
Modifier.align(...) | One child only | When one item needs special alignment | modifier = Modifier.align(Alignment.CenterHorizontally) |
vs
Cheat Sheet
// Center children horizontally in a Column
Column(
modifier = Modifier.fillMaxWidth(),
horizontalAlignment = Alignment.CenterHorizontally
) { ... }
// Center children vertically and horizontally
Column(
modifier = Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) { ... }
// Center only one child
Text(
"Hello",
modifier = Modifier.align(Alignment.CenterHorizontally)
)
Rules to remember
Columnstacks children vertically.horizontalAlignmentcontrols child alignment left-to-right.verticalArrangementcontrols placement top-to-bottom.- Vertical centering needs available height.
- Horizontal centering usually needs
fillMaxWidth(). - Use
Modifier.align(...)for per-child alignment.
Quick answer for this case
Column(
modifier = Modifier.fillMaxWidth(),
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(text = item.title)
Text(text = item.description)
}
FAQ
How do I center text inside a Jetpack Compose Column?
Use horizontalAlignment = Alignment.CenterHorizontally on the Column, and usually add Modifier.fillMaxWidth().
Why is Arrangement.Center not working in my Column?
Because the Column may not have extra height. Add Modifier.fillMaxHeight() or Modifier.fillMaxSize().
What is the difference between horizontalAlignment and verticalArrangement in a Column?
horizontalAlignment centers each child left-to-right. verticalArrangement centers or spaces the group top-to-bottom.
How do I center only one item in a Column?
Use Modifier.align(Alignment.CenterHorizontally) on that specific child.
Is gravity used in Jetpack Compose like in XML layouts?
Mini Project
Description
Build a simple empty-state screen in Jetpack Compose. The screen should display a title and a subtitle centered in the middle of the screen. This demonstrates how Column alignment works in a realistic UI scenario that appears frequently in Android apps.
Goal
Create a centered empty-state layout using a Column with proper horizontal and vertical alignment.
Requirements
- Create a
Columnthat takes the full available screen space. - Center the child elements horizontally.
- Center the group of child elements vertically.
- Display two
Textcomposables: a title and a subtitle.
Keep learning
Related questions
Accessing Kotlin Extension Functions from Java
Learn how Kotlin extension functions are compiled and how to call them correctly from Java with clear examples and common pitfalls.
Allow HTTP and HTTPS in Android 9 Pie with Network Security Configuration
Learn how Android 9 Pie handles cleartext HTTP traffic and how to allow HTTP and HTTPS safely using network security config.
Android AlarmManager Example: Scheduling Tasks with AlarmManager
Learn how to use Android AlarmManager to schedule tasks, set alarms, and handle broadcasts with a simple beginner example.