Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jetpack Compose - Column - Gravity center

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)  } 
like image 657
mac229 Avatar asked Jan 13 '20 08:01

mac229


People also ask

How do you center a column in jetpack compose?

To center align content of Column along horizontal axis in Android Compose, set horizontalAlignment parameter with the value of Alignment. CenterHorizontally . Also, we may fill the maximum width by the Column using Modifier. fillMaxWidth().

How do you center align text in jetpack compose?

If you want only to center horizontally just use: Column( modifier = Modifier. fillMaxWidth(), horizontalAlignment = Alignment.

What is LazyColumn in jetpack compose?

A LazyColumn is a vertically scrolling list that only composes and lays out the currently visible items. It's similar to a Recyclerview in the classic Android View system.


2 Answers

You can use these parameters:

  • horizontalAlignment = the horizontal gravity of the layout's children.
  • verticalArrangement= the vertical arrangement of the layout's children

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

like image 137
Gabriele Mariotti Avatar answered Sep 19 '22 22:09

Gabriele Mariotti


Use this

   Column(         horizontalAlignment = Alignment.CenterHorizontally,         verticalArrangement = Arrangement.Center     ) 
like image 39
Nana kTk Avatar answered Sep 21 '22 22:09

Nana kTk