One Off the Slack: Centering Complications

One of the challenges in Jetpack Compose development in these early days is the continuously-changing API.

On April 23rd, Shreeman Arjun Sahu asked:

How to place a Text in center in latest update dev 09 version? There is no composable function Center(deprecated).

As it turns out, there are many ways not to do it. 😅

One way that works was pointed out by Alex Zhukovich:

Text(
    text = "abc",
    modifier = Modifier.fillMaxWidth() + Modifier.wrapContentSize(Alignment.Center)
)

Later, Google’s Adam Powell suggested:

Text(
    text = "abc",
    modifier = Modifier.fillMaxSize().wrapContentSize(Alignment.Center)
)

In both cases, we tell the text to fill the available space, then tell it to have its content be center-aligned within that space. Adam’s solution takes advantage of the intrinsic chainable API style of Modifier, to avoid the need for the + operator and repeating the Modifier name.


Read the original thread in the kotlinlang Slack workspace. Not a member? Join that Slack workspace here!