Zhang Zihan
11/06/2025, 11:08 AMCircularProgressIndicator centered when there are no items in a LazyVerticalGrid.
In a `LazyColumn`'s LazyItemScope, this can be easily achieved using `Modifier.fillParentMaxSize`:
LazyColumn {
item {
Text("Some header", modifier = Modifier.fillMaxWidth())
}
item {
Box(modifier = Modifier.fillParentMaxSize(), contentAlignment = Alignment.Center) {
CircularProgressIndicator()
}
}
}
However, Modifier.fillParentMaxSize is not available in a LazyGridItemScope of a `LazyVerticalGrid`:
LazyVerticalGrid(columns = GridCells.Adaptive(160.dp)) {
item(span = { GridItemSpan(maxLineSpan) }) {
Text("Some header")
}
item {
Box(modifier = Modifier.fillParentMaxSize(), contentAlignment = Alignment.Center) {
^^^^^^^^^^^^^^^^^ Unresolved reference 'fillParentMaxSize'.
CircularProgressIndicator()
}
}
}
Is there any alternative way to achieve the same effect?Stylianos Gakis
11/06/2025, 12:25 PMBox() {
LazyVerticalGrid() { ... }
if (noItemsInGrid) { CircularProgressIndicator(Modifier.align(Alignment.Center)) }
}Zhang Zihan
11/06/2025, 12:34 PMLazyVerticalGrid has a header that is always displayed, the indicator displayed by this way is not centered in the blank area.
I used a similar alternative way to achieve this:
if (items == null) {
LazyColumn {
item {
Text("Some header", modifier = Modifier.fillMaxWidth())
}
item {
Box(modifier = modifier.fillParentMaxSize(), contentAlignment = Alignment.Center) {
CircularProgressIndicator()
}
}
}
} else {
LazyVerticalGrid(...) {
item(span = { GridItemSpan(maxLineSpan) }) {
Text("Some header")
}
items(items) { ... }
}
}