Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Building Screens with Jetpack Compose  ›  Capstone Project

Cards and Lazy Lists

Card 16 minA Card groups one item's content on a raised surface, and LazyColumn shows long lists efficiently by composing only the items that are visible.

Cards and Lazy Lists

Sprout's home screen is a list of plants, each on its own card. With five plants anything works. With three hundred, the wrong choice makes scrolling stutter. This lesson brings together everything in the section: text, icons, layouts and state, inside a fast list.

The rule

A Card holds one thing; a lazy list only builds what you can see.

Card is a Material surface with rounded corners and a subtle elevation or outline. It groups one item's content, and its inside is a normal content slot:

@Composable
fun PlantCard(plant: Plant, onClick: () -> Unit) {
    Card(onClick = onClick, modifier = Modifier.fillMaxWidth()) {
        Column(Modifier.padding(16.dp)) {
            Text(plant.name, style = MaterialTheme.typography.titleMedium)
            Text("Water every ${plant.days} days")
        }
    }
}

LazyColumn is the list container. A plain Column composes every child immediately, including ones far below the screen. LazyColumn composes only the items currently visible (plus a few nearby) and creates more as the user scrolls. LazyRow does the same horizontally.

LazyColumn(contentPadding = PaddingValues(16.dp)) {
    items(plants, key = { it.id }) { plant ->
        PlantCard(plant, onClick = { open(plant.id) })
    }
}
  • items(list) creates one entry per element.
  • key gives each entry a stable identity, so when a plant is added or removed, the others keep their state and animate correctly.

Note that you cannot call composables directly inside LazyColumn's lambda: it is a list builder, not a composable body, so entries are added with items or item.

The shortcut

Ask "could this list grow beyond one screen?" If yes, use LazyColumn with items and a key. If it is a short, fixed set, a Column is fine.

Sprout's plant listStep 1 of 4
  1. One item's look

    Name and schedule on a raised surface → PlantCard built on Card.

Drills

Answer fast — trust the rule.

quick drill

Why is LazyColumn better than Column for 300 plants?

quick drill

Inside LazyColumn { }, how do you add one entry per plant?

quick drill

What does key = { it.id } give each list entry?

quick drill

A settings screen always shows the same four options. Which container is enough?

Remember

  • A Card holds one thing; a lazy list only builds what you can see.
  • Growing lists → LazyColumn with items and a key.
  • Short, fixed content → a plain Column is fine.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainA hundred plants

A user with 300 plants says Sprout's list is slow. The list is a Column that loops over all plants and draws a Card for each. Explain the problem and describe the fix, including what items and key do.