4

My Problem:

I want to place my Text() content to center of the page in Scaffold.

I tried "textAlign = TextAlign.Center" - It align the text in horizontal area alone. Not align the text in vertical area.

My code:

@Composable fun ScaffoldWithTopBar() { Scaffold( topBar = { TopAppBar( title = { Text(text = "Top App Bar") }, navigationIcon = { IconButton(onClick = {}) { Icon(Icons.Filled.ArrowBack, "backIcon") } }, backgroundColor = MaterialTheme.colors.primary, contentColor = Color.White, elevation = 10.dp ) }, content = { Column( modifier = Modifier .fillMaxSize() .background(Color(0xff8d6e63)), verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally ) { } Text( text = "Content of the page", fontSize = 30.sp, color = Color.White ) }) } 

Note: I haven't place this text into Column. I used directly.

My output:

enter image description here

Question: How to place the text into the center of the parent?

1

4 Answers 4

9

Assign the fillMaxSize() to the parent container, not to the Text:

 Box(Modifier.fillMaxSize(), contentAlignment = Center ) { Text( text = "Content of the page", fontSize = 30.sp, modifier = Modifier .background(Color(0xffb3e5fc)), ) } 

or:

Column(Modifier.fillMaxSize(), verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally ) { Text( text = "Content of the page", fontSize = 30.sp, modifier = Modifier .background(Color(0xffb3e5fc)), ) } 

enter image description here

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

4 Comments

Thanks! You are helping our community. My doubt - without container (Column) it will not work?
@RanjithKumar No
I updated code in my question. If possible can you check my code?
@RanjithKumar in the new code, you are using the Text outside the Column
3

You need to place it inside a Box, and specify contentAlignment

Box( contentAlignment = Alignment.Center, modifier = Modifier .fillMaxSize() ) { Text( text = "Content of the page", fontSize = 30.sp, modifier = Modifier .background(Color(0xffb3e5fc)), ) } 

If you wanna align different Box children differently, you can use .align modifier for each item. Note that it's only available inside BoxScope, also there's same modifier for Column and Row.

Box( modifier = Modifier .fillMaxSize() ) { Text( text = "Content of the page", textAlign = TextAlign.Center, fontSize = 30.sp, modifier = Modifier .background(Color(0xffb3e5fc)) .align(Alignment.Center) ) } 

2 Comments

align modifier not available in compose version 1.0.1. Without any container it will not work ?
@RanjithKumar yes, it's BoxScope modifier, you still need to place your item inside a Box
1
Box( modifier = Modifier .fillMaxSize() ) { Text( text = stringResource(id = R.string.social_list_empty), style = TextStyle(fontSize = 16.sp), modifier = Modifier .padding(8.dp) .align(Alignment.Center), textAlign = TextAlign.Center ) } 

enter image description here

Reference : For more sample codes of Jetpack Compose, check this

Comments

0

The content in Scaffold is a Composable function in which there is no position info for items by default. In your case, the default position of Text is the top left of its parent view. The solutions putting the Text inside the Box/Colum works, as now Text is put in the center of the Box or Colum, which takes the whole screen. We see Text is in the center of the screen, while the truth is Text is in the center of Box or Colum.

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.