LazyRow helps you show a list of items side by side that you can scroll horizontally. It only creates items you see on screen, so it saves memory and runs smoothly.
LazyRow for horizontal lists in Android Kotlin
LazyRow(
modifier = Modifier.fillMaxWidth(),
contentPadding = PaddingValues(horizontal = 16.dp),
horizontalArrangement = Arrangement.spacedBy(8.dp)
) {
items(yourList) { item ->
Text(text = item)
}
}LazyRow is a composable that shows items horizontally and loads only visible items.
You use items() inside LazyRow to provide the list data and how each item looks.
LazyRow {
items(listOf("Apple", "Banana", "Cherry")) { fruit ->
Text(text = fruit)
}
}LazyRow(
horizontalArrangement = Arrangement.spacedBy(12.dp)
) {
items(emptyList<String>()) { item ->
Text(text = item)
}
}LazyRow {
items(listOf("One")) { item ->
Text(text = item)
}
}LazyRow {
items(listOf("Start", "Middle", "End")) { item ->
Text(text = item)
}
}This app shows a horizontal list of fruit names you can scroll left or right. The fruits are spaced nicely with padding on the sides.
import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.lazy.LazyRow 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.unit.dp class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { MaterialTheme { Surface(modifier = Modifier.fillMaxWidth()) { FruitLazyRow() } } } } } @Composable fun FruitLazyRow() { val fruits = listOf("Apple", "Banana", "Cherry", "Date", "Elderberry") LazyRow( modifier = Modifier.fillMaxWidth(), contentPadding = PaddingValues(horizontal = 16.dp), horizontalArrangement = Arrangement.spacedBy(12.dp) ) { items(fruits) { fruit -> Text(text = fruit) } } }
LazyRow only creates items visible on screen, so it uses less memory than a regular Row with many items.
Use contentPadding to add space at the start and end of the list.
Common mistake: Forgetting to use items() inside LazyRow causes errors.
LazyRow shows horizontal lists efficiently by loading only visible items.
Use items() to provide the list and how each item looks.
It is perfect for scrollable horizontal menus, photos, or tags.
Practice
LazyRow in Android Compose?Solution
Step 1: Understand LazyRow's role
LazyRow is designed to show horizontal lists efficiently by loading only the items visible on screen.Step 2: Compare with other options
Options A, B, and C describe unrelated features: navigation, vertical lists, and input handling, not LazyRow's purpose.Final Answer:
To display a horizontally scrollable list that loads only visible items -> Option AQuick Check:
LazyRow = horizontal efficient list [OK]
- Confusing LazyRow with LazyColumn (vertical list)
- Thinking LazyRow manages navigation
- Assuming LazyRow handles input events
itemsList?Solution
Step 1: Recall LazyRow usage with items()
LazyRow uses theitems()function to iterate over a list and display each item.Step 2: Check syntax correctness
LazyRow { items(itemsList) { item -> Text(item) } } correctly usesitems(itemsList) { item -> Text(item) }. Options B, C, and D use invalid or unsupported syntax for LazyRow.Final Answer:
LazyRow { items(itemsList) { item -> Text(item) } } -> Option CQuick Check:
LazyRow + items() = LazyRow { items(itemsList) { item -> Text(item) } } [OK]
- Passing list directly as parameter to LazyRow
- Using for loops inside LazyRow instead of items()
- Misspelling items() or using wrong variable names
val colors = listOf("Red", "Green", "Blue")
LazyRow {
items(colors) { color ->
Text(text = color)
}
}Solution
Step 1: Analyze the code behavior
The code creates a LazyRow with items from the colors list, displaying each color as Text horizontally.Step 2: Evaluate options
A horizontal scrollable row showing Red, Green, Blue texts correctly describes a horizontal scrollable row showing the color names. A vertical list showing Red, Green, Blue is wrong because LazyRow is horizontal, not vertical. An error because colors is immutable is wrong; immutable lists are fine. Nothing, because LazyRow requires a modifier is incorrect; modifier is optional.Final Answer:
A horizontal scrollable row showing Red, Green, Blue texts -> Option BQuick Check:
LazyRow + items(colors) = horizontal list [OK]
- Thinking LazyRow creates vertical lists
- Assuming immutable lists cause errors
- Believing modifier is mandatory for display
LazyRow {
items(listOf("A", "B", "C")) { item ->
Text item
}
}Solution
Step 1: Check Text composable usage
Text is missing parentheses:Text itemshould beText(item).Step 2: Verify other parts
items() can accept listOf() directly, and modifier is optional.Final Answer:
Missing parentheses in Text composable -> Option DQuick Check:
Text(item) syntax required [OK]
- Missing parentheses in Text(item)
- Thinking items() requires modifier parameter
- Assuming listOf() cannot be used inline
Solution
Step 1: Filter the list before passing to items()
LazyRow { items((1..5).filter { it % 2 == 0 }) { Text(text = it.toString()) } } filters the range to only even numbers, so items() receives only 2 and 4.Step 2: Check other options for correctness
LazyRow { items(1..5) { Text(text = it.toString()) } } displays all numbers 1-5. LazyRow { items(1..5) { Text(text = if (it % 2 == 0) it.toString() else "odd") } } displays 'odd' for odd numbers. LazyRow { items((1..5).map { if (it % 2 == 0) it else null }) { Text(text = it.toString()) } } maps odd numbers to null, which causes runtime errors.Final Answer:
LazyRow { items((1..5).filter { it % 2 == 0 }) { Text(text = it.toString()) } } -> Option AQuick Check:
Filter list before items() = LazyRow { items((1..5).filter { it % 2 == 0 }) { Text(text = it.toString()) } } [OK]
- Forgetting the filtering condition entirely
- Mapping to null causing errors
- Displaying placeholder text for unwanted items
Ready to prove it?
Pass the test and this pattern moves unknown
