29
votes

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)
 }
16

16 Answers

63
votes

With 1.0.0 you can use these parameters:

Something 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)
        )
}

enter image description here

If you want only to center horizontally just use:

Column(
        modifier = Modifier.fillMaxWidth(),
        horizontalAlignment = Alignment.CenterHorizontally
) {
    Column (  ) { ... }

enter image description here

4
votes

You can use Column(crossAxisAlignment = CrossAxisAlignment.Center) . It works like gravity.

4
votes

You have the option to apply gravity on the individual items as follows then it will center the items.

Column(modifier = ExpandedWidth) {
       Text(modifier = Gravity.Center, text = item.title)
       Text(modifier = Gravity.Center, text = item.description)
}
1
votes

You can use

    Text(
        text = item.title,
        textAlign = TextAlign.Center,
        modifier = Modifier.align(alignment = Alignment.CenterHorizontally)
    )
  • textAlign is for aligning text inside the box.
  • Modifier.align() is for aligning text box inside column
1
votes

In case if you are using lazyColumn to load list and want make whole item center you can use this

LazyColumn( horizontalAlignment = Alignment.CenterHorizontally) {

}
1
votes

Use this

   Column(
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center
    )
0
votes

You can use Arrangement.Center

  • Place child components as close to the center of the main axis

SAMPLE CODE

@Composable
fun Column(

    arrangement: Arrangement = Arrangement.Center,

)
0
votes

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)
    }
}
0
votes

This is how you do it as of the latest version of compose(0.1.0-dev05)

For you to center the text inside this container, you need to use the LayoutAlign modifier.

Column(modifier = LayoutWidth.Fill + LayoutAlign.Center) {
    Text(text = item.title)
    Text(text = item.description)
}
0
votes

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))
    }
}
0
votes

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))
    }
}
0
votes

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)
 }
0
votes

use

modifier = Modifier.align(Alignment.CenterHorizontally)
0
votes

I don't like it too much, but this is what it worked to me:

Column(modifier = Modifier.fillMaxWidth().fillMaxHeight(),
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.Center) {
                //Your composable elements
            }

Compose 1.0.0-beta07

-1
votes

You can set align in Row or Column fir Text Composable in alpha07

Text(text = "someText" , modifier = Modifier.align(Alignment.CenterVertically))
-1
votes
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