In Jetpack Compose, when using Text
, we can center text using TextAlign
:
Text(
text = "How many cars are in the garage",
textAlign = TextAlign.Center
)
But if we're looking to get the position of a click within a Text composable, we use ClickableText
:
ClickableText(
text = AnnotatedString("Click Me"),
onClick = { offset ->
Log.d("ClickableText", "$offset -th character is clicked.")
}
)
However I don't see how to center text in ClickableText? I can use gravity, but that will only center the component, not the text inside of it.
You can define the textAlign
in the style
parameter:
ClickableText(
text = AnnotatedString("Click Me"),
style = TextStyle(
textAlign = TextAlign.Center),
onClick = { offset ->
}
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With