Creating Horizontal Layouts with Row in Jetpack Compose

Understanding Layouts in Jetpack Compose: The "Row"

In this guide, we'll explore the Row composable, the essential tool for placing UI elements side-by-side in Jetpack Compose.


What is a "Row"?

A Row is a layout composable that arranges its children in a horizontal sequence, from left to right (or right to left, depending on the locale). It is the direct equivalent of a LinearLayout with android:orientation="horizontal" from the classic XML world.

  • Stacks UI elements horizontally.
  • Fundamental for creating components like toolbars, item listings with icons, and button bars.
  • Works in perfect harmony with Column to build any layout you can imagine.
@Composable
fun SimpleRowExample() {
    // Everything inside this Row's lambda will be placed side-by-side.
    Row {
        Text("Item 1")
        Text("Item 2")
        Text("Item 3")
    }
}

When to Use a "Row"?

Any time you need to place elements next to each other, Row is the answer. Common use cases include:

  • An Icon followed by a Text label.
  • A user's avatar Image next to their username.
  • A set of action buttons like "Like", "Comment", and "Share".

💡 Tip: Complex UIs are just combinations of Row, Column, and another layout called Box. Mastering Row and Column gets you 90% of the way there.


Customizing a "Row"

Just like Column, a Row's behavior is customized through its parameters. However, the axes are now swapped!

  • Main Axis: Horizontal
  • Cross Axis: Vertical

This means horizontalArrangement controls spacing along the row, and verticalAlignment controls how items are aligned up and down within the row.

@Composable
fun StyledRowExample() {
    Row(
        // The Modifier is used to give size, padding, etc.
        modifier = Modifier
            .fillMaxWidth()      // Make the Row take up all available width.
            .padding(16.dp)
            .background(Color.LightGray), // Added for visibility

        // "horizontalArrangement" controls spacing and placement on the HORIZONTAL axis.
        horizontalArrangement = Arrangement.SpaceEvenly,

        // "verticalAlignment" controls alignment on the VERTICAL axis.
        verticalAlignment = Alignment.CenterVertically
    ) {
        Text("Hello")
        Button(onClick = { /* Do something */ }) {
            Text("Click Me")
        }
        Icon(imageVector = Icons.Default.Favorite, contentDescription = "Favorite Icon")
    }
}

"Arrangement" vs. "Alignment" for a "Row"

Let's clarify this, as it's the most important concept:

  • horizontalArrangement (The Main Axis): This decides how items are spaced out horizontally.
    • Arrangement.Start: Clump items to the left (default).
    • Arrangement.Center: Group items in the horizontal center.
    • Arrangement.End: Clump items to the right.
    • Arrangement.SpaceBetween: Place items evenly, with space between them.
    • Arrangement.SpaceAround: Place items with equal space around each of them.
  • verticalAlignment (The Cross Axis): This decides where each item sits vertically within the row.
    • Alignment.Top: Align items to the top of the row.
    • Alignment.CenterVertically: Center each item vertically (most common).
    • Alignment.Bottom: Align items to the bottom of the row.

Nesting Layouts: Combining "Row" and "Column"

The true power of Compose layouts is realized when you combine them. Here's a simple example of a user profile card where a "Row" is placed inside a "Column".

@Composable
fun UserProfileCard() {
    Column(modifier = Modifier.padding(16.dp)) {
        // First element in the Column is a Row
        Row(
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.spacedBy(16.dp)
        ) {
            // Image and Text are inside the Row
            Image(
                painter = painterResource(id = R.drawable.user_avatar), // Add an avatar to your drawables
                contentDescription = "User Avatar"
            )
            Text("GrayLight User")
        }
        
        // A Spacer for some vertical distance
        Spacer(modifier = Modifier.height(8.dp))

        // Second element in the Column
        Text("Bio: Exploring the world of Jetpack Compose!")
    }
}

Previewing Your "Row"

Don't forget to use @Preview to see your horizontal layouts come to life instantly. It's the best way to experiment with different arrangements and alignments.

@Preview(showBackground = true)
@Composable
fun PreviewStyledRow() {
    YourAppNameTheme {
        StyledRowExample()
    }
}

Key Concepts Recap

  • Row: A layout composable that arranges its children horizontally, side-by-side.
  • horizontalArrangement: Controls spacing and distribution of children along the main (horizontal) axis for a Row.
  • verticalAlignment: Controls the placement of children along the cross (vertical) axis for a Row.
  • Nesting: You can place Rows inside Columns (and vice-versa) to build any UI you need.

Popular posts from this blog

Foundations of Jetpack Compose and Kotlin