Compose Layout: Row and Column in Jetpack Compose
Jetpack Compose introduces a new, declarative way of designing Android UIs. Instead of using XML layouts, Compose lets you build UIs directly in Kotlin code, enabling faster iteration and improved code readability. The core components in this layout system are Row
and Column
, which allow developers to arrange UI elements horizontally or vertically. This article will cover the basics of Row
and Column
, their modifiers, and how to achieve complex layouts with alignment and spacing.
Basics of Row
and Column
in Jetpack Compose
Row
: Lays out children horizontally, one next to the other.Column
: Lays out children vertically, stacking them from top to bottom.
These composables are foundational to Compose layouts, making it easy to position elements precisely as needed.
Using Row
for Horizontal Layouts
A Row
aligns items horizontally and is useful for side-by-side positioning of elements like icons with text, buttons, or lists of items.
import androidx.compose.foundation.layout.Row
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview
@Composable
fun RowExample() {
Row {
Text("Item 1")
Text("Item 2")
Text("Item 3")
}
}
@Preview
@Composable
fun PreviewRowExample() {
RowExample()
}
In this example, three Text
composables are aligned horizontally within a Row
. Each item is placed next to the previous one.
Using Column
for Vertical Layouts
A Column
aligns items vertically, making it ideal for stacking components like labels, input fields, and buttons.
import androidx.compose.foundation.layout.Column
@Composable
fun ColumnExample() {
Column {
Text("Item A")
Text("Item B")
Text("Item C")
}
}
@Preview
@Composable
fun PreviewColumnExample() {
ColumnExample()
}
This example demonstrates a vertical stack of Text
items. Each Text
composable is placed below the previous one.
Customizing Row
and Column
with Modifier
In Jetpack Compose, Modifier
is used to customize UI components, including setting size, padding, margins, background color, alignment, and click events. Applying a Modifier
to Row
or Column
enables control over how they display and position their children.
Adding Padding and Spacing
Padding can be added to a Row
or Column
to create space around its children. Arrangement
and Alignment
help control spacing and alignment of items within the layout.
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.ui.unit.dp
@Composable
fun PaddedRowExample() {
Row(
modifier = Modifier.padding(16.dp),
horizontalArrangement = Arrangement.SpaceBetween
) {
Text("Start")
Text("Center")
Text("End")
}
}
Modifier.padding
: Adds padding around the entire row.horizontalArrangement
: Controls the spacing between items in theRow
. In this case,SpaceBetween
distributes space evenly between items.
Aligning Items in Row
and Column
Alignment options allow you to control the positioning of items inside a Row
or Column
.
Horizontal Alignment in Row
Within a Row
, you can use the verticalAlignment
parameter to position items along the vertical axis.
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.ui.Alignment
@Composable
fun AlignedRowExample() {
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceEvenly
) {
Text("Top")
Text("Center")
Text("Bottom")
}
}
In this example, verticalAlignment = Alignment.CenterVertically
centers each item along the vertical axis.
Vertical Alignment in Column
In a Column
, the horizontalAlignment
parameter is used to align items horizontally.
@Composable
fun AlignedColumnExample() {
Column(
modifier = Modifier.padding(16.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
Text("Left")
Text("Center")
Text("Right")
}
}
In this Column
, all Text
composables are centered horizontally.
Arrangements and Spacing in Row
and Column
Jetpack Compose offers built-in Arrangement
options to control spacing in Row
and Column
:
Arrangement.Start
: Aligns items to the start.Arrangement.End
: Aligns items to the end.Arrangement.Center
: Centers items within the layout.Arrangement.SpaceBetween
: Distributes space evenly between items.Arrangement.SpaceAround
: Adds space around each item.Arrangement.SpaceEvenly
: Distributes space evenly before, after, and between items.
Using Arrangement in Column
import androidx.compose.foundation.layout.Arrangement
@Composable
fun SpacedColumnExample() {
Column(
verticalArrangement = Arrangement.SpaceAround,
modifier = Modifier.padding(16.dp)
) {
Text("Item 1")
Text("Item 2")
Text("Item 3")
}
}
In this example, Arrangement.SpaceAround
creates equal space around each item within the Column
.
Nesting Row
and Column
for Complex Layouts
Compose layouts become more powerful when you nest Row
and Column
components to create complex UI structures.
@Composable
fun NestedLayoutExample() {
Column(
modifier = Modifier.padding(16.dp)
) {
Row(
horizontalArrangement = Arrangement.SpaceBetween,
modifier = Modifier.fillMaxWidth()
) {
Text("Title")
Text("Subtitle")
}
Column(
verticalArrangement = Arrangement.spacedBy(8.dp),
modifier = Modifier.padding(top = 16.dp)
) {
Text("Item A")
Text("Item B")
Text("Item C")
}
}
}
In this layout:
- The first
Row
contains a title and subtitle aligned horizontally. - The inner
Column
contains multiple items stacked vertically, with space between each item.
Handling Scrollable Content
For layouts with many items, you can wrap Row
or Column
in a ScrollableRow
or ScrollableColumn
(or LazyRow
and LazyColumn
for more efficient handling).
import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.rememberScrollState
@Composable
fun ScrollableRowExample() {
Row(
modifier = Modifier
.horizontalScroll(rememberScrollState())
.padding(16.dp)
) {
for (i in 1..10) {
Text(text = "Item $i", modifier = Modifier.padding(8.dp))
}
}
}
In this example, the Row
can scroll horizontally. This is particularly useful for layouts with a large number of items that need to be viewed one by one.
Summary
The Row
and Column
composables in Jetpack Compose are essential building blocks for creating both simple and complex layouts. Key features include:
Row
: For horizontal layouts, with control over spacing and vertical alignment.Column
: For vertical layouts, with options for horizontal alignment.- Modifiers: Customize layout behavior using
Modifier
for padding, size, background, and click handling. - Arrangement and Alignment: Control item placement within the layout.
By combining Row
and Column
, along with modifiers and nested layouts, you can achieve a wide range of UI designs in Jetpack Compose. Mastering these composables will allow you to structure layouts with precision and adapt easily to various UI requirements.