Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stack Text vertically with rotation using Jetpack Compose?

Trying to align text elements as shown in the image below:

enter image description here

How to do it in Jetpack Compose?

Normally I would use a Linear Layout with vertical orientation and TextViews with a rotation of 90. Wondering how to achieve this in compose.

like image 446
Kalyan Dechiraju Avatar asked Jul 16 '26 03:07

Kalyan Dechiraju


2 Answers

compose_verion: 1.0.0-beta02

To rotate an element you can use Modifier.rotate() modifier

Column {
     Text(text = "text", modifier = Modifier.rotate(-90f))
     Text(text = "text", modifier = Modifier.rotate(-90f))
     Text(text = "text", modifier = Modifier.rotate(-90f))
}
like image 60
Mahdi-Malv Avatar answered Jul 18 '26 19:07

Mahdi-Malv


The solution with a custom modifier from How to show vertical text with proper size/layout in jetpack compose worked for me:

fun Modifier.vertical() = layout { measurable, constraints ->
    val placeable = measurable.measure(constraints)
    layout(placeable.height, placeable.width) {
        placeable.place(
            x = -(placeable.width / 2 - placeable.height / 2),
            y = -(placeable.height / 2 - placeable.width / 2)
        )
    }
}

Used as

Text(
    modifier = Modifier.vertical().rotate(-90f),
    text = "Vertical aligned element"
)

Credits to Sergei S's answer.

like image 36
malliaridis Avatar answered Jul 18 '26 18:07

malliaridis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!