Short answer: to build a grid in Jetpack Compose, use LazyVerticalGrid with a columns parameter: GridCells.Fixed(n) for a set number of columns, or GridCells.Adaptive(minSize) to fit as many columns as the screen allows. It replaces GridView from the View system and only renders visible items.
Basic grid with fixed columns
@Composable
fun SimpleGrid(items: List<String>) {
LazyVerticalGrid(
columns = GridCells.Fixed(2),
contentPadding = PaddingValues(16.dp),
horizontalArrangement = Arrangement.spacedBy(12.dp),
verticalArrangement = Arrangement.spacedBy(12.dp)
) {
items(items) { label ->
Card(
modifier = Modifier
.fillMaxWidth()
.aspectRatio(1f)
) {
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
Text(label)
}
}
}
}
}
// Usage
SimpleGrid(items = List(20) { "Item ${it + 1}" })
Import androidx.compose.foundation.lazy.grid.LazyVerticalGrid, GridCells and androidx.compose.foundation.lazy.grid.items.
Adaptive columns
Adaptive cells adjust the number of columns to the screen width, which is useful on phones, tablets and foldables:
LazyVerticalGrid(
columns = GridCells.Adaptive(minSize = 128.dp),
contentPadding = PaddingValues(16.dp),
horizontalArrangement = Arrangement.spacedBy(12.dp),
verticalArrangement = Arrangement.spacedBy(12.dp)
) {
items(photos, key = { it.id }) { photo ->
PhotoCard(photo)
}
}
Each column is at least minSize wide, and Compose adds columns as space allows.
Make an item span several columns
Use span for headers or banners that fill the full row:
LazyVerticalGrid(columns = GridCells.Fixed(3)) {
item(span = { GridItemSpan(maxLineSpan) }) {
Text("Section title", style = MaterialTheme.typography.titleLarge)
}
items(photos, key = { it.id }) { photo ->
PhotoCard(photo)
}
}
Staggered grid
If your items have different heights, such as a Pinterest-style layout, use LazyVerticalStaggeredGrid with StaggeredGridCells.Fixed(2) or Adaptive. It works like LazyVerticalGrid but lets rows have uneven heights.
Grid vs Column and Row
| Need | Use |
|---|---|
| One long scrolling list | LazyColumn (see LazyColumn list example) |
| Rows and columns of equal cells | LazyVerticalGrid |
| Horizontal grid | LazyHorizontalGrid |
| Small, fixed set of items | Plain Row and Column, with scrolling if needed |
Common mistakes
- Nesting a LazyVerticalGrid inside a vertically scrolling Column. This crashes because the grid gets infinite height. Use one scrolling container, or place the grid in the same scroll area using
item(span = ...). - Skipping item keys. Provide a stable
keyso items keep their state when the list changes. - Using fixed columns on every screen size. Prefer
Adaptivefor responsive layouts. - Loading large images without a library. Use an image loading library such as Coil and give image cells a fixed size or aspect ratio.
FAQ
What replaces GridView in Jetpack Compose?
LazyVerticalGrid (and LazyHorizontalGrid for horizontal grids).
What is the difference between GridCells.Fixed and Adaptive?
Fixed always uses the number of columns you give. Adaptive uses as many columns as fit given a minimum column width.
How do I add spacing between grid items?
Use horizontalArrangement and verticalArrangement with Arrangement.spacedBy(...), plus contentPadding around the whole grid.
Can grid items have different sizes?
Use span for items that cover several columns, or LazyVerticalStaggeredGrid for items with different heights.
More: Jetpack Compose tutorials.