Compose has three primitive layouts and you can build almost anything from them. Column stacks children vertically, Row places them horizontally, and Box overlays them. Each takes arrangement and alignment parameters that control spacing along the main axis and position along the cross axis.
For long lists, LazyColumn replaces Column. It composes only the items currently visible, which is the Compose equivalent of RecyclerView - putting a thousand items in a plain Column composes all thousand and will visibly stutter. Give list items a stable key so that reordering moves them rather than rebuilding them.
Modifiers decorate a composable: size, padding, background, click handling, and much more. They are applied in a chain, and the order matters because each wraps the previous one. Modifier.padding(16.dp).background(Red) paints red inside the padding; Modifier.background(Red).padding(16.dp) paints red outside it. This trips up nearly everyone once.
Sizing follows a constraint model: a parent passes constraints down, a child measures itself within them, and the parent places it. fillMaxWidth takes all the width offered, wrapContentSize takes only what is needed, and weight inside a Row or Column divides the remaining space proportionally.
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
@Composable
fun NoteScreen(titles: List<String>) {
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.spacedBy(8.dp),
) {
// Modifier ORDER matters: background is painted outside this padding
Text(
"Notes",
modifier = Modifier
.background(Color.LightGray)
.padding(16.dp),
)
Row(verticalAlignment = Alignment.CenterVertically) {
Text("Left", modifier = Modifier.weight(1f)) // takes the slack
Button(onClick = { }) { Text("Add") }
}
// LazyColumn composes only what is on screen; key keeps identity stable
LazyColumn(modifier = Modifier.weight(1f)) {
items(titles, key = { it }) { title ->
ListItem(headlineContent = { Text(title) })
}
}
}
}
Discussion
No questions on this topic yet. Sign in to ask the first one.