How Do I Display Tasks in a LazyColumn with Jetpack Compose?

In article 2, you created a Task model and three sample tasks. The screen displayed only the first task. In this article, you will display the entire collection as a scrolling list and create a reusable component for each task.

Continue with the same Task Notes project, package org.kodejava.tasknotes, and generated TaskNotesTheme. Keep Task.kt and SampleTasks.kt from the previous article unchanged.

1. Choose a layout for the list

Column arranges composables vertically. LazyColumn provides a vertically scrolling list whose items are composed and laid out as needed. It also supports prefetching, so avoid assuming that only the exact visible rows can exist in composition.

Three fixed rows could fit in a regular Column. We use LazyColumn because Task Notes will eventually contain a growing collection of user-created tasks. See the official lazy lists documentation.

We will keep the screen title above the list so that it remains visible while the tasks scroll.

2. Replace MainActivity.kt

Replace MainActivity.kt with the complete example below. If you use another package or generated theme name, adjust those declarations and imports.

File: app/src/main/java/org/kodejava/tasknotes/MainActivity.kt

package org.kodejava.tasknotes

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Card
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import org.kodejava.tasknotes.data.sampleTasks
import org.kodejava.tasknotes.model.Task
import org.kodejava.tasknotes.ui.theme.TaskNotesTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()

        setContent {
            TaskNotesTheme {
                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                    TaskNotesScreen(
                        tasks = sampleTasks(),
                        modifier = Modifier.padding(innerPadding)
                    )
                }
            }
        }
    }
}

@Composable
private fun TaskNotesScreen(
    tasks: List<Task>,
    modifier: Modifier = Modifier
) {
    Column(
        modifier = modifier.fillMaxSize(),
        verticalArrangement = Arrangement.spacedBy(8.dp)
    ) {
        Text(
            text = "Task Notes",
            style = MaterialTheme.typography.headlineMedium,
            modifier = Modifier.padding(
                start = 24.dp,
                top = 24.dp,
                end = 24.dp
            )
        )
        Text(
            text = "Tasks: ${tasks.size}",
            style = MaterialTheme.typography.bodyMedium,
            modifier = Modifier.padding(horizontal = 24.dp)
        )

        if (tasks.isEmpty()) {
            Text(
                text = "No tasks yet.",
                modifier = Modifier.padding(24.dp)
            )
        } else {
            LazyColumn(
                modifier = Modifier.fillMaxWidth().weight(1f),
                contentPadding = PaddingValues(
                    horizontal = 24.dp,
                    vertical = 16.dp
                ),
                verticalArrangement = Arrangement.spacedBy(12.dp)
            ) {
                items(
                    items = tasks,
                    key = { task -> task.id }
                ) { task ->
                    TaskRow(task = task)
                }
            }
        }
    }
}

@Composable
private fun TaskRow(
    task: Task,
    modifier: Modifier = Modifier
) {
    Card(modifier = modifier.fillMaxWidth()) {
        Column(
            modifier = Modifier.padding(16.dp),
            verticalArrangement = Arrangement.spacedBy(6.dp)
        ) {
            Text(
                text = task.title,
                style = MaterialTheme.typography.titleMedium
            )
            Text(
                text = if (task.isCompleted) "Completed" else "Not completed",
                style = MaterialTheme.typography.bodyMedium,
                color = MaterialTheme.colorScheme.onSurfaceVariant
            )
        }
    }
}

@Preview(showBackground = true)
@Composable
private fun TaskNotesScreenPreview() {
    TaskNotesTheme {
        TaskNotesScreen(tasks = sampleTasks())
    }
}

@Preview(showBackground = true)
@Composable
private fun EmptyTaskNotesScreenPreview() {
    TaskNotesTheme {
        TaskNotesScreen(tasks = emptyList())
    }
}

weight(1f) is available here through the surrounding Column scope. You do not need to add a separate weight import for this example.

3. Read the list code

The screen now accepts tasks: List<Task> as an argument. Its caller supplies sample data, while the screen describes how to display it. The same screen can therefore show the original samples, an empty list, or a larger collection without changing its implementation.

Inside LazyColumn, items() describes the repeated content:

items(
    items = tasks,
    key = { task -> task.id }
) { task ->
    TaskRow(task = task)
}

The items argument is the collection. The final lambda receives one task and describes its row. Import androidx.compose.foundation.lazy.items to use the collection overload.

Each key identifies an item across list changes. Our model’s distinct Long IDs remain suitable even when titles change or tasks move. Do not use the title as a key: two tasks can have the same title. A key must remain stable for that task and be unique within the list.

Keys do not save task data to disk or make a read-only list interactive. We will introduce observable state in the next article.

4. Understand the row and spacing

TaskRow() receives one Task and displays its title and status inside a Material 3 Card. The status uses words, so the user does not have to interpret a color to know whether a task is completed.

The row has no click handler or checkbox yet. Completion still comes from the values in SampleTasks.kt.

The layout uses several kinds of spacing:

Code Purpose
Modifier.padding(innerPadding) Keeps screen content clear of system bars through the existing Scaffold
weight(1f) Gives the list the remaining height below the header
contentPadding Adds space before, after, and beside the scrolling rows
Arrangement.spacedBy(12.dp) Adds space between task cards
Modifier.padding(16.dp) Adds space inside each card

Do not wrap this screen in a vertically scrolling Column. The list already scrolls, and it needs a bounded height. Here, the activity supplies the screen bounds and weight(1f) allocates the remaining space to the list.

5. Check the screen

Run the app on your emulator or phone. It should show Task Notes, Tasks: 3, and these cards:

Title Status
Learn Kotlin basics Not completed
Create the Task model Completed
Build the task list Not completed

All three cards may fit without scrolling. To check scrolling, temporarily replace tasks = sampleTasks() in MainActivity.onCreate() with:

tasks = List(50) { index ->
    Task(
        id = index.toLong() + 1L,
        title = "Practice task ${index + 1}",
        isCompleted = index % 3 == 0
    )
},

Keep the following modifier argument. Kotlin’s List(50) creates fifty elements using indexes from zero to forty-nine. Each generated task receives a different ID. The % operator returns the remainder, so indexes divisible by three produce completed tasks.

Run again, scroll to Practice task 50, and confirm that the header stays above the scrolling list. Restore tasks = sampleTasks() afterward.

To check the empty state, temporarily pass emptyList() instead. The count should become zero and the screen should show No tasks yet. Restore the original samples when finished.

The two preview functions also let you inspect populated and empty screens. This article’s Android build and device behavior have not been executed in an Android SDK environment; the checks above are steps to perform in your project.

Troubleshooting

  • If items expects an integer instead of your list, check the androidx.compose.foundation.lazy.items import.
  • If Compose reports duplicate keys, check that every task has a distinct ID. Repeating the same sample list without changing IDs creates duplicates.
  • If scrolling fails with an infinite-height constraint error, remove an outer vertical scroll container and keep the bounded layout shown above.
  • If your generated theme has another name, use that name in both the activity and previews.

Practice

  1. Add a fourth sample task with ID 4L and verify the count and new card.
  2. Give one task a longer title. Check that it wraps without hiding the status.
  3. Change the card spacing from 12.dp to 20.dp and compare the result.

Restore the original three samples before continuing.

Gradle dependencies and compatibility

No additional Gradle dependencies are required when continuing from articles 1 and 2. The example uses the existing Compose Foundation, Material 3, and preview dependencies.

Keep the plugin versions, Compose BOM, and version catalog generated for your working project. This lesson does not require a dependency upgrade. For configuration details, refer to the official Compose setup guide.

Unlike a Maven Java project, this Android project manages its dependencies through Gradle. There is no Maven dependency block to add for this article.

References

Next in the series

In article 4, How Do I Update Task Completion with State in Jetpack Compose?, you will add a checkbox and update a task’s completion status using observable state and copy().

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.