193

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

19 Answers 19

341

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

Sign up to request clarification or add additional context in comments.

6 Comments

if you are using a Row, any idea align them so one composable function is at the start and another at the center? something like spaceEvenly in flexbox in css or like in Row widget in flutter api.flutter.dev/flutter/widgets/Row-class.html you can use mainAxisAlignment.spaceEvenly
I was getting error on align, but I update the jetpack compose dependency to 1.0.0-alpha03 and it worked. Previously it was 1.0.0-alpha02 thank you!
You can simply use Modifier.fillMaxSize() instead of Modifier.fillMaxWidth().fillMaxHeight()
@GabrieleMariotti Maybe off topic, but how would you center the text "TopAppBar" in your image? That's actually why I am here :)
|
108

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

3 Comments

Modifier.align is only for BoxScope
textAlign is the correct answer
textAlign = TextAlign.Center is correct. Note that it doesn't belong to modifier.
29

Column view's alignings

You can find details in attachment by looking figures of all possibilities of aligning view inside column. Hope it helps you!

    //View Center Horizontally Only
    
    Column(modifier =  Modifier.fillMaxSize(),horizontalAlignment = Alignment.CenterHorizontally){
                
//code
                
}
        
        
    //View Center Vertically Only
    
    Column(modifier = Modifier.fillMaxSize(),verticalArrangement = Arrangement.Center){
                
    //code
                
    }
        
        
    //View Center Horizontally and Vertically
    
    Column(modifier = Modifier.fillMaxSize(),verticalArrangement = Arrangement.Center,horizontalAlignment = Alignment.CenterHorizontally){
                
    //code
                
    }

1 Comment

The screenshots may be helpful, but per Why should I not upload images of code/data/errors?, the code should still be text so it's accessible.
13

Use this

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

2 Comments

This one is definitely the one to use, Last version of Composable 1.0.0 and more
Isn't this the same as the accepted answer, except with less detail?
13

Alternatively you can create your own composable and use it everywhere:

@Composable
fun Center( modifier: Modifier = Modifier, content: @Composable ColumnScope.() -> Unit) {
    Column(
        modifier = modifier.fillMaxSize(),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally,
        content = content
    )
}

And you use like this:

Center {
    Text(text = "Text 1")
    Text(text = "Text 2")
}

Comments

11
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

2 Comments

This has already been mentioned in other answers, such as stackoverflow.com/a/66391295/2227743
to test set heightIn(min = 40.dp) and see that it is just centred horizontally
7
 Column(
     modifier = Modifier.fillMaxSize(),
     verticalArrangement = Arrangement.Center,
     horizontalAlignment = Alignment.CenterHorizontally
     ){
       // Child Composable Here Like Text, Row, Box, Image ... more 
     }

verticalArrangement -> Make all Child Composable Center Vertically to Column(Parent)

horizontalAlignment -> Make all Child Composable Center Horizontally to Column(Parent)

1 Comment

Isn't this the same as the accepted answer, except with less detail?
6

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

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

Compose 1.0.0-beta07

2 Comments

Modifier.fillMaxSize() instead of Modifier.fillMaxWidth().fillMaxHeight()
Isn't this the same as the accepted answer, except with less detail?
5

You can also use textAlign property of Text composable

Text(
    modifier = Modifier.fillMaxWidth(),
    text = item.title,
    textAlign = TextAlign.Center
)
Text(
    modifier = Modifier.fillMaxWidth(),
    text = item.description,
    textAlign = TextAlign.Center
)

Comments

4

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,

)

Comments

4

if you are using 0.1.0-dev09 you can modify the gravity or arrangement attributes by using the parameters horizontalAlignment and verticalArrangement

@Composable
fun centeredColumn() {
    return Column (
        modifier = Modifier.height(100.dp), 
        horizontalAlignment = Alignment.CenterHorizontally, 
        verticalArrangement = Arrangement.Center
    ) {
            Image(asset = imageResource(id = R.drawable.ic_emojo_logo_white))
            Text(text = stringResource(id = R.string.login_subtitle))
    }
}

1 Comment

Isn't this the same as the accepted answer, except with less detail?
3

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

5 Comments

This answer is deprecated, as Text doesn't allow Gravity now
Text(text = item.title, modifier = Modifier.gravity(Alignment.CenterHorizontally))
With alpha07, use Text(text = item.title, modifier = Modifier.align(Alignment. CenterHorizontally))
Modifier align for Text doesn't exists for 1.0.0-beta09version of Compose.
The suggestions above don't work (at least with Compose 1.0.0) but this does: style = TextStyle(textAlign = TextAlign.Center)
3

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 Comment

This seems identical to many other existing answers.
2

use

modifier = Modifier.align(Alignment.CenterHorizontally)

2 Comments

Works great with buttons!
This seems identical to many other existing answers.
1

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

Comments

1

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

Comments

0

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

Comments

0

Update: This answer is outdated.

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

2 Comments

unfortunately it's not available in dev-03
Related to Flutter not JetpackCompose
-4
Text(
    textAlign = TextAlign.Center,
    text = "We are making some improvements. Please, try again later."
)

1 Comment

Thank you for your interest in contributing to the Stack Overflow community. This question already has quite a few answers—including one that has been extensively validated by the community. Are you certain your approach hasn’t been given previously? If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.