Skip to content

Jetpack Compose LazyGrid — Complete Guide

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about Jetpack Compose LazyGrid. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

The Problem

Your grid has uneven cell sizes, items overlap, or scrolling stutters because you're using regular Column/Row loops.

Wrong Approach ❌

@Composable
fun BadGrid(items: List<String>) {
    // Manual grid with Row/Column — all items composed upfront
    Column {
        items.chunked(2).forEach { row ->
            Row {
                row.forEach { item ->
                    Text(item, modifier = Modifier.weight(1f))
                }
            }
        }
    }
}

Output: 1000 items all rendered at once — OutOfMemoryError.

Right Approach ✅

@Composable
fun GoodGrid(items: List<String>) {
    LazyVerticalGrid(
        columns = GridCells.Adaptive(minSize = 128.dp),
        contentPadding = PaddingValues(8.dp),
        horizontalArrangement = Arrangement.spacedBy(8.dp),
        verticalArrangement = Arrangement.spacedBy(8.dp)
    ) {
        items(
            items = items,
            key = { it }
        ) { item ->
            Card(
                modifier = Modifier.fillMaxWidth(),
                colors = CardDefaults.cardColors()
            ) {
                Text(
                    text = item,
                    modifier = Modifier.padding(16.dp),
                    maxLines = 2,
                    overflow = TextOverflow.Ellipsis
                )
            }
        }

        // Fixed-span items (header spanning all columns)
        item(span = { GridItemSpan(maxLineSpan) }) {
            Text("Header", style = MaterialTheme.typography.headlineSmall)
        }
    }
}

// Fixed number of columns
LazyVerticalGrid(
    columns = GridCells.Fixed(3),
    modifier = Modifier.fillMaxSize()
) { /* ... */ }

Output: Smooth lazy grid with proper span and spacing.

Prevention

  • Use LazyVerticalGrid with GridCells.Fixed or GridCells.Adaptive.
  • Use GridCells.Adaptive(minSize) for responsive column counts.
  • Always provide stable key values.
  • Use span = { GridItemSpan(maxLineSpan) } for full-width items.

Common Mistakes with compose lazy grid

  1. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
  2. Using return to exit a function early instead of wrapping a pure value in the monad
  3. Mixing let bindings with <- bindings in do notation, producing type errors

These mistakes appear frequently in real-world Android code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What is the difference between GridCells.Fixed and GridCells.Adaptive?

Fixed(3) creates exactly 3 columns. Adaptive(128.dp) creates as many 128dp-wide columns as fit — automatically responsive to screen width.

### Can I use LazyVerticalGrid with horizontal scrolling?

For horizontal grids, use LazyHorizontalGrid (available in Compose Foundation 1.5+). The API is symmetric to the vertical version.

### How do I handle empty space in the last row?

A grid naturally fills left-to-right, top-to-bottom. Empty slots at the end are left blank. Use Modifier.weight(1f) within each cell if you want cells to fill the row.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro