LazyColumn helps you show a long list of items efficiently by loading only what is visible on screen.
LazyColumn with items in Android Kotlin
Introduction
Syntax
Android Kotlin
LazyColumn {
items(list) { item ->
Text(text = item)
}
}LazyColumn creates a vertical scrolling list.
items() takes a list and a lambda to show each item.
Examples
Android Kotlin
val fruits = listOf("Apple", "Banana", "Cherry") LazyColumn { items(fruits) { fruit -> Text(text = fruit) } }
Android Kotlin
LazyColumn {
items(5) { index ->
Text(text = "Item #$index")
}
}Sample App
This program shows a vertical scrollable list of fruit names using LazyColumn and items.
Android Kotlin
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() } }
Important Notes
LazyColumn only creates items when they appear on screen, saving memory.
You can customize each item with any composable, not just Text.
Remember to import androidx.compose.foundation.lazy.items to use the items function.
Summary
LazyColumn shows vertical scrolling lists efficiently.
Use items() to display each element from a list.
It helps keep your app smooth with large lists.
Practice
1. What is the main purpose of using
LazyColumn in Android Jetpack Compose?easy
Solution
Step 1: Understand LazyColumn's role
LazyColumn is designed to show vertical lists efficiently by loading only the items visible on screen.Step 2: Compare options
Options A, B, and C are incorrect: A handles input events, B creates horizontal lists, C is for grids.Final Answer:
To display a vertically scrolling list efficiently by loading only visible items -> Option AQuick Check:
LazyColumn = Efficient vertical list [OK]
Hint: LazyColumn loads only visible items for smooth scrolling [OK]
Common Mistakes:
- Confusing LazyColumn with LazyRow (horizontal list)
- Thinking LazyColumn creates grids
- Assuming it handles input events
2. Which of the following is the correct syntax to display a list of strings named
names inside a LazyColumn using items?easy
Solution
Step 1: Recall correct function name
The function to display each element in a list inside LazyColumn isitems, notitem,itemsOf, oritemsList.Step 2: Check lambda syntax
The lambda receives each element (herename) and displays it withText(name).Final Answer:
LazyColumn { items(names) { name -> Text(name) } } -> Option BQuick Check:
Use items() with list and lambda [OK]
Hint: Use items() to iterate list inside LazyColumn [OK]
Common Mistakes:
- Using item() instead of items() for lists
- Wrong function names like itemsOf or itemsList
- Incorrect lambda parameter usage
3. What will be displayed by this code snippet?
val fruits = listOf("Apple", "Banana", "Cherry")
LazyColumn {
items(fruits) { fruit ->
Text(text = fruit)
}
}medium
Solution
Step 1: Understand items usage
Theitemsfunction iterates over thefruitslist and displays each element inside the LazyColumn.Step 2: Check UI behavior
LazyColumn shows a vertical scrolling list, so each fruit appears on its own line vertically.Final Answer:
A vertical list showing Apple, Banana, Cherry each on its own line -> Option DQuick Check:
items() + LazyColumn = vertical list [OK]
Hint: LazyColumn with items shows vertical list of all elements [OK]
Common Mistakes:
- Thinking LazyColumn creates horizontal lists
- Assuming only first item shows
- Confusing syntax causing no output
4. Identify the error in this LazyColumn code:
val numbers = listOf(1, 2, 3)
LazyColumn {
items(numbers) { number ->
Text(number)
}
}medium
Solution
Step 1: Check Text() parameter type
TheTextcomposable expects a String, butnumberis an Int, causing a type mismatch error.Step 2: Confirm correct usage of items()
Theitemsfunction accepts lists of any type, so no error there.Final Answer:
Text() expects a String, but number is Int causing a type mismatch -> Option AQuick Check:
Text() needs String, not Int [OK]
Hint: Convert Int to String inside Text() to fix [OK]
Common Mistakes:
- Assuming items() rejects Int lists
- Using item() instead of items() for lists
- Ignoring type mismatch in Text()
5. You want to display a list of user names with their index number in a LazyColumn. Which code correctly shows the index and name together?
hard
Solution
Step 1: Identify function for index and item
To get both index and item, useitemsIndexedwhich provides both parameters in the lambda.Step 2: Check lambda parameters and usage
LazyColumn { itemsIndexed(users) { index, user -> Text("$index: $user") } } correctly usesindex, userand displays them with string interpolation.Final Answer:
LazyColumn { itemsIndexed(users) { index, user -> Text("$index: $user") } } -> Option CQuick Check:
Use itemsIndexed() for index + item [OK]
Hint: Use itemsIndexed() to get index and item in LazyColumn [OK]
Common Mistakes:
- Using items() when index is needed
- Wrong lambda parameters count
- Trying to access index without itemsIndexed()
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
