Issue
Older versions of Jetpack compose dev-0.x
used to have a Center
composable to center a widget.
However, in the alpha
version it has been removed and no specific composable made to handle this task.
In my case I want to show a progress indicator at the center until data is fetched:
Surface(color = MaterialTheme.colors.background) {
val state = viewModel.userState
if (state == null) {
CircularProgressIndicator()
} else {
UserDigest(state = state)
}
}
The result is obviously something like this:
And the indicator is at the corner.
Adding fillMaxSize()
to the Indicator does not help either:
CircularProgressIndicator(modifier = Modifier.fillMaxSize())
So, how do I move it (generally a composable
) to the center of an area?
Solution
You have to use a parent container.
For example:
Box(
contentAlignment = Alignment.Center,
modifier = Modifier.fillMaxSize()
) {
CircularProgressIndicator()
}
or:
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
CircularProgressIndicator()
}
Answered By - Gabriele Mariotti
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.