31

How can I align the text using Text composable function vertically. Is there a way to do it without having to add another extra view to contain the Text.

The textAlign parameter of Text only has the following options:

TextAlign.

  • Left
  • Right
  • Center
  • Justify
  • Start
  • End

I have tried using textAlign = TextAlign.Center but it only centers it horizontally. How can I center it vertically without wrapping it in another view?

Text( text = "Text", modifier = Modifier.size(100.dp), textAlign = TextAlign.Center ) 

Result:

result

What I am trying to achieve:

what I am trying to achieve

3
  • Post your code and an image of what you are trying to achieve. Commented Aug 30, 2021 at 15:53
  • no, you can't do that without a container. Commented Aug 30, 2021 at 16:33
  • Gabriele Mariotti, I edited the question Commented Aug 31, 2021 at 1:00

5 Answers 5

41

You have to use a parent container and align the composable inside it.

For example a Box:

Box( modifier = Modifier.fillMaxSize(), contentAlignment = Center ) { Text( text = "Text", ) } 

or a Column:

Column( modifier = Modifier.fillMaxSize(), verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally ) { Text( text = "Text", ) } 
Sign up to request clarification or add additional context in comments.

4 Comments

"Type mismatch. Required: Alignment.Horizontal Found: Arrangement.HorizontalOrVertical"
VerticalArrangement doesn't seem to do anything.
@IgorGanapolsky The verticalArrangement defines the vertical arrangement of the layout's children in the Column. Check also the height of the parent container.
Which import are you using for contentAlignment = Center?
12

first use .wrapContentHeight() in modifier, then you can use align(CenterVertically) param to center the text.

Comments

6

It is difficult to give an exact answer without seeing your code, but I would say you need to use container's Alignment.CenterVertically instead of TextAlign

Something like:

Row { Text( text = "Text", modifier = Modifier.align(alignment = Alignment.CenterVertically) ) Image( ... ) } 

or:

Column( modifier = Modifier .align(Alignment.CenterVertically) ) { Text(text = "Text") Text(text = "Text 2") } 

Comments

4

I asked the same question here which is done with android:gravity param using views, as answered by PhilipDukhov it's not possible only with Text, you need to have another container, preferably Box to align Text component inside.

Comments

1

Create a simple composable function to show anything in center:

@Composable fun Center( modifier: Modifier = Modifier, content: @Composable () -> Unit, ) { Box( modifier = modifier, contentAlignment = Alignment.Center ) { content() } } 

Call it from any other function:

@Composable fun CenterAlignedText() { Center(Modifier.fillMaxSize()) { Text("Hello World!") } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.