Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jetpack Compose - how to center text in ClickableText

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.

like image 510
haart Avatar asked Sep 12 '25 16:09

haart


1 Answers

You can define the textAlign in the style parameter:

ClickableText(
    text = AnnotatedString("Click Me"),
    style = TextStyle(
        textAlign = TextAlign.Center),
    onClick = { offset ->

    }
)
like image 90
Gabriele Mariotti Avatar answered Sep 15 '25 04:09

Gabriele Mariotti