LazyColumn helps you show a long list of items on the screen without slowing down your app. It only draws what you see, saving memory and making scrolling smooth.
LazyColumn for lists in Android Kotlin
LazyColumn {
items(listOfItems) { item ->
Text(text = item)
}
}LazyColumn is a composable that shows a vertical scrolling list.
The items function takes a list and a lambda to show each item.
LazyColumn {
items(listOf("Apple", "Banana", "Cherry")) { fruit ->
Text(text = fruit)
}
}LazyColumn {
items(emptyList<String>()) { item ->
Text(text = item)
}
}LazyColumn {
items(listOf("Only one item")) { item ->
Text(text = item)
}
}This program shows a vertical scrolling list of fruit names using LazyColumn. Each fruit name is shown as text. The list fills the available screen space.
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Surface import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.ui.tooling.preview.Preview @Composable fun FruitList() { val fruits = listOf("Apple", "Banana", "Cherry", "Date", "Elderberry") LazyColumn(modifier = Modifier.fillMaxSize()) { items(fruits) { fruit -> Text(text = fruit, style = MaterialTheme.typography.bodyLarge) } } } @Preview(showBackground = true) @Composable fun FruitListPreview() { Surface { FruitList() } }
LazyColumn only creates items that are visible on screen, so it uses less memory and is faster than a regular Column for long lists.
Time complexity for displaying items is O(n) where n is visible items, not the whole list.
Common mistake: Using a regular Column for long lists can cause slow app and crashes due to memory.
Use LazyColumn when you have many items or an unknown number of items to show efficiently.
LazyColumn shows vertical lists efficiently by creating only visible items.
Use it for long or dynamic lists to keep your app smooth and fast.
Remember to pass your list to items inside LazyColumn to display each element.
Practice
LazyColumn in Android Jetpack Compose?Solution
Step 1: Understand LazyColumn behavior
LazyColumn creates only the visible items on screen, not all at once.Step 2: Compare with other list types
Unlike loading all items, LazyColumn keeps the app smooth by loading items lazily.Final Answer:
To efficiently display a vertical list by creating only visible items -> Option AQuick Check:
LazyColumn = efficient vertical list [OK]
- Thinking LazyColumn loads all items at once
- Confusing LazyColumn with horizontal lists
- Assuming LazyColumn creates grid layouts
items using LazyColumn?Solution
Step 1: Identify correct function name
The function to iterate over a list in LazyColumn isitems, notitem,itemsList, orlist.Step 2: Check lambda usage
The lambda receives each element asitand displays it withText(it).Final Answer:
LazyColumn { items(items) { Text(it) } } -> Option CQuick Check:
Use items() inside LazyColumn [OK]
- Using item() instead of items() for lists
- Misspelling items as itemsList or list
- Not passing the list variable correctly
val names = listOf("Anna", "Bob", "Cara")
LazyColumn {
items(names) { name ->
Text(text = name)
}
}Solution
Step 1: Analyze the list and items call
The list contains three names: Anna, Bob, and Cara. The items function iterates over them.Step 2: Understand LazyColumn behavior
LazyColumn displays items vertically by default, so each name will appear as a separate Text item vertically.Final Answer:
A vertical list showing Anna, Bob, and Cara -> Option AQuick Check:
LazyColumn + items = vertical list [OK]
- Thinking LazyColumn creates horizontal lists
- Expecting no output without explicit item count
- Confusing runtime errors with missing parameters
val numbers = listOf(1, 2, 3)
LazyColumn {
items(numbers) { number ->
Text(number)
}
}Solution
Step 1: Check Text parameter type
The Text composable expects a String, butnumberis an Int, causing a type mismatch.Step 2: Fix by converting Int to String
UseText(number.toString())to convert the number to a String before displaying.Final Answer:
Text expects a String, but number is an Int -> Option DQuick Check:
Text needs String, not Int [OK]
- Using items instead of item for single element
- Assuming LazyColumn can't show numbers
- Forgetting to convert Int to String for Text
Solution
Step 1: Add header as a single item
Useitem { Text("Users") }to add a single header before the list.Step 2: Add list items after header
Useitems(userList) { user -> Text(user) }to display each user below the header.Final Answer:
LazyColumn { item { Text("Users") } items(userList) { user -> Text(user) } } -> Option BQuick Check:
Use item() for header, items() for list [OK]
- Placing header after list items
- Trying to combine header and list in one items call
- Placing Text outside LazyColumn scope
Ready to prove it?
Pass the test and this pattern moves unknown
