I'm creating a layout with Jetpack Compose and there is a column. I would like center items inside this column:
Column(modifier = ExpandedWidth) {
Text(text = item.title)
Text(text = item.description)
}
With 1.0.0 you can use these parameters:
horizontalAlignment = the horizontal gravity of the layout's children.verticalArrangement= the vertical arrangement of the layout's childrenSomething like:
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(
text = "First item",
modifier = Modifier.padding(16.dp)
)
Text(
text = "Second item",
modifier = Modifier.padding(16.dp)
)
Text(
text = "Third item",
modifier = Modifier.padding(16.dp)
)
}
If you want only to center horizontally just use:
Column(
modifier = Modifier.fillMaxWidth(),
horizontalAlignment = Alignment.CenterHorizontally
) {
Column ( ) { ... }
You could use FlexColumn to bring content in the center as it supports CrossAxisAlignment.
FlexColumn(crossAxisAlignment = CrossAxisAlignment.Center) {
inflexible {
Text(text = item.title)
Text(text = item.description)
}
}
Warp it inside inflexible so that widgets will not occupy the full screen.
In the same way if you want to bring views to the center of the screen with FlexColumn than use mainAxisAlignment along with crossAxisAlignment
FlexColumn(mainAxisAlignment = MainAxisAlignment.Center,
crossAxisAlignment = CrossAxisAlignment.Center) {
inflexible {
Text(text = item.title)
Text(text = item.description)
}
}
if you are using 0.1.0-dev09 you can modify the gravity or arrangement attributes by using the parameters horizontalGravity and verticalArrangement
@Composable
fun centeredColumn() {
return Column (
modifier = Modifier.height(100.dp),
horizontalGravity = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
Image(asset = imageResource(id = R.drawable.ic_emojo_logo_white))
Text(text = stringResource(id = R.string.login_subtitle))
}
}
You can use Box , by default it aligns it childs in a ColumnScope, you can think of Box like a Container for vertically aligned Views if you want to align any component instead of making each time a Column to align components
@Composable
fun BoxDemo(){
Box(modifier = Modifier.fillMaxSize(),gravity = Alignment.Center){
Text(text = "First text",modifier = Modifier.padding(top = 8.dp))
Text(text = "Second text",modifier = Modifier.padding(top = 8.dp))
Text(text = "Third text",modifier = Modifier.padding(top = 8.dp))
}
}
To center horizontally
Column(modifier = Modifier.fillMaxWidth()) {
Text(text = item.title)
Text(text = item.description)
}
To center both horizontally and vertically in the parent surface
Column(modifier = Modifier.fillMaxWidth().fillMaxHeight(), verticalArrangement = Arrangement.Center) {
Text(text = item.title)
Text(text = item.description)
}
Text(
"Hello World",
textAlign = TextAlign.Center,
modifier = Modifier.width(150.dp)
)
You can use TextAlign attribute to center the texts.
Reference - https://developer.android.com/jetpack/compose/text