1

Is it possilble as of 1.0.0-alpha03 to align text center vertically without nesting components Stack -> Text

 Text( modifier = Modifier .size(150.dp, 30.dp) .background(Colors.ChipGray, RoundedCornerShape(50)), textAlign = TextAlign.Center, text = start.value.format(DateTimeFormatter.ofPattern("dd. MM. HH:mm")), ) 
1
  • Do to want to align the text vertically inside the Text() component? Commented Sep 20, 2020 at 15:27

1 Answer 1

1

In General, it is possible but I would not suggest it.

You could use offset to shift the text relative to the background by half of the font size, but I am not sure if you can access the height of the used font here.

 Text( text = start.value.format(DateTimeFormatter.ofPattern("dd. MM. HH:mm")), textAlign = TextAlign.Center, modifier = Modifier .size(150.dp, 30.dp) .background(Color.Gray, RoundedCornerShape(50)) .offset(y = fontHeightHalf) ) 

Use the stack instead, like:

 Stack ( alignment = Alignment.Center, modifier = Modifier .size(150.dp, 30.dp) .background(Color.Gray, RoundedCornerShape(50)), ) { Text( text = start.value.format(DateTimeFormatter.ofPattern("dd. MM. HH:mm")), textAlign = TextAlign.Center ) } 

Edit: Updated version to 1.0.0-alpha03

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

2 Comments

Thank you so much for a clarification. With a Stack there is even better way as of 1.0.0-alpha03. You can set align parameter directly on the Stack. android-review.googlesource.com/c/platform/frameworks/support/+/… BTW: the gravity modifer was replaced by align
That is nice, makes it a little bit clearer. I updated the answer to the newer version, I was still running 1.0.0-alpha01.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.