142

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)
 }
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
mac229
  • 4,319
  • 5
  • 18
  • 24
  • Jetpack compose is wonderful, but lacking some ready-made helpful components. I have assembled a set of free composeables that developers can use freely, or developers can submit their composeables to share. There are currently 15 very helpful composeables there, with screen shots, code etc. http://composeables.com/ – pstorli Jul 19 '23 at 02:28

18 Answers18

265

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

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • 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 https://api.flutter.dev/flutter/widgets/Row-class.html you can use mainAxisAlignment.spaceEvenly – Peter Haddad Sep 24 '20 at 12:59
  • @PeterHaddad https://developer.android.com/reference/kotlin/androidx/compose/foundation/layout/ColumnScope#align and check this post: https://joebirch.co/android/exploring-jetpack-compose-column – Gabriele Mariotti Sep 24 '20 at 14:09
  • 1
    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! – Peter Haddad Sep 24 '20 at 16:26
  • You can simply use `Modifier.fillMaxSize()` instead of `Modifier.fillMaxWidth().fillMaxHeight()` – YektaDev Jun 05 '21 at 13:33
  • @GabrieleMariotti Maybe off topic, but how would you center the text "TopAppBar" in your image? That's actually why I am here :) – Rafael Jan 28 '22 at 08:55
  • @Rafael Check https://stackoverflow.com/questions/67497414/how-to-align-title-at-layout-center-in-topappbar/67499847#67499847 – Gabriele Mariotti Jan 28 '22 at 09:24
  • 1
    Finally somebody with a reasonable answer. Thanks! – Joaquin Iurchuk Jul 01 '22 at 21:40
70

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
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
Bincy Baby
  • 3,941
  • 5
  • 36
  • 62
16

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!

Syed Ibrahim
  • 356
  • 2
  • 6
11

Use this

   Column(
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center
    )
Nana kTk
  • 221
  • 3
  • 4
8
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

Majid Hajibaba
  • 3,105
  • 6
  • 23
  • 55
vibhor chinda
  • 91
  • 1
  • 1
  • 1
    This has already been mentioned in other answers, such as https://stackoverflow.com/a/66391295/2227743 – Eric Aya Jun 06 '21 at 20:10
  • to test set `heightIn(min = 40.dp)` and see that it is just centred horizontally – Amir Jul 26 '21 at 00:20
8

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")
}
Amin Keshavarzian
  • 3,646
  • 1
  • 37
  • 38
4

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

ruif3r
  • 101
  • 2
  • 4
3

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,

)
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
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)
}
Klaas Kabini
  • 167
  • 4
3

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
)
Jim Ovejera
  • 739
  • 6
  • 10
2

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))
    }
}
David Rawson
  • 20,912
  • 7
  • 88
  • 124
markcadag
  • 271
  • 5
  • 3
2

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

LazyColumn( horizontalAlignment = Alignment.CenterHorizontally) {

}
SagaRock101
  • 129
  • 1
  • 7
1

use

modifier = Modifier.align(Alignment.CenterHorizontally)
Ryan M
  • 18,333
  • 31
  • 67
  • 74
EunhaEonnie
  • 223
  • 2
  • 4
0

Update: This answer is outdated.

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

ankuranurag2
  • 2,300
  • 15
  • 30
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)
    }
}
Pankaj
  • 131
  • 1
  • 3
0

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)
}
Vinay Gaba
  • 1,156
  • 3
  • 12
  • 26
0

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)
 }
KingKongCoder
  • 600
  • 4
  • 15
0
 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)

Hasan Ali
  • 31
  • 2