Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing Long press on Text Jetpack compose

I have a LazyColumn of Text(). I've set clickable for Text() but it's equivalent to OnClickListner. Now I want to set an equivalent of setOnLongClickListener. How can I do that?

@Composable
fun MyText(name: String, modifier: Modifier = Modifier) {

  var isSelected by remember {
        mutableStateOf(false)
    }
        Text(
            text = "Hello $name!",
            modifier = modifier
                .clickable { isSelected = !isSelected }
                .padding(16.dp)
        )
like image 631
TinaTT2 Avatar asked May 10 '26 08:05

TinaTT2


1 Answers

You can use the combinedClickable modifier to get the different click events:

Text(
    text = text,
    modifier = Modifier
        .combinedClickable(
            onLongClick = { /*....*/ },
            onClick ={ /*....*/ })
        .padding(16.dp)
)

Notice that it is an @ExperimentalFoundationApi feature and is likely to change or be removed in the future.

like image 187
Gabriele Mariotti Avatar answered May 13 '26 02:05

Gabriele Mariotti



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!