Jetpack Compose: Getting Started

Aug 1 2023 · Kotlin 1.8.10, Android 13, Android Studio Flamingo

Part 2: Layout Your App

07. Columns & Rows

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: Part 1 Quiz: Make a Simple Interface Next episode: 08. Add Spacers

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

We have previously used the Column and Row Composables to arrange Composables vertically and horizontally respectively. In this session, we will dive a little deeper into how to align items within a Column and Row.

Column {
    // Composables
}

Column

@Composable
fun ColumnDemo() {
    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(Color.LightGray),
        verticalArrangement = Arrangement.SpaceEvenly,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Text(
            text = "Item 1",
            modifier = Modifier
                .background(Color.Green)
                .padding(16.dp)
        )
        Text(
            text = "Item 2",
            modifier = Modifier
                .background(Color.Blue)
                .padding(16.dp)
        )
        Text(
            text = "Item 3",
            modifier = Modifier
                .background(Color.Red)
                .padding(16.dp)
        )
    }
}

We can modify the position of the Item 2 by using the align Modifier function. This function is part of the ColumnScope interface.

@Composable
fun ColumnDemo() {
    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(Color.LightGray),
        verticalArrangement = Arrangement.SpaceEvenly,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Text(
            text = "Item 1",
            modifier = Modifier
                .background(Color.Green)
                .padding(16.dp)
        )
        Text(
            text = "Item 2",
            modifier = Modifier
                .background(Color.Blue)
                .padding(16.dp)
                .align(Alignment.End)
        )
        Text(
            text = "Item 3",
            modifier = Modifier
                .background(Color.Red)
                .padding(16.dp)
        )
    }
}
Row {
    // Composables
}

Row

@Composable
fun RowDemo() {
    Row(
        modifier = Modifier
            .fillMaxSize()
            .background(Color.LightGray),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.SpaceEvenly
    ) {
        Text(
            text = "Item 1",
            modifier = Modifier
                .background(Color.Green)
                .padding(16.dp)
        )
        Text(
            text = "Item 2",
            modifier = Modifier
                .background(Color.Blue)
                .padding(16.dp)
        )
        Text(
            text = "Item 3",
            modifier = Modifier
                .background(Color.Red)
                .padding(16.dp)
        )
    }
}

We can modify the position of the Item 2 by using the align Modifier function. This function is part of the RowScope interface.


@Composable
fun RowDemo() {
    Row(
        modifier = Modifier
            .fillMaxSize()
            .background(Color.LightGray),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.SpaceEvenly
    ) {
        Text(
            text = "Item 1",
            modifier = Modifier
                .background(Color.Green)
                .padding(16.dp)
        )
        Text(
            text = "Item 2",
            modifier = Modifier
                .background(Color.Blue)
                .padding(16.dp)
                .align(Alignment.Bottom)
        )
        Text(
            text = "Item 3",
            modifier = Modifier
                .background(Color.Red)
                .padding(16.dp)
        )
    }
}