0

How to detect a long click

    Button(
    onClick = onClick,
    interactionSource = interactionSource,
    modifier = Modifier
        .combinedClickable(
            onClick = onClick,
            onLongClick = onLongClick
        )
)

This doesn't work. onClick is required and I guess it consumes combinedClickable. I also need an interactionSource. I would rather prefer to avoid create a custom button.

kkkkk
  • 572
  • 1
  • 10
  • 21
  • Works for me. Try putting some brackets around your functions, like: ```onLongClick = { onLongClick }```? Not sure if that would help... – Code Poet Jun 03 '23 at 09:16
  • I've added just logs for an action. And still I got only an event from main onClick. combinedClickable does nothing. – kkkkk Jun 03 '23 at 09:20

2 Answers2

1

It won't work with Button because of the same reason you can't chain Modifier.clickable{}.clickable{}

With Button adding another Modifier.pointerInput() won't have any effect because Button is a Surface with onClick param that is assigned Modifier.clickable() already. Modifier.clickable already consumes events and not letting other Modifier.pointerInput() receiving it.

In Jetpack Compose when there are two or more Modifier.pointerInput() chained together the one at the bottom receives event first with default pass which isPointeEventPass.Main. Since, clickable already consumes, the one you assign will never get it.

Simplest way is to create a Box, Surface uses Box under the hood, and set your combinedClickable.

You can check out this answer for consume, event propagation and how gestures work under the hood if interested in details.

https://stackoverflow.com/a/70847531/5457853

Thracian
  • 43,021
  • 16
  • 133
  • 222
  • Other option even if it's unnecessary, if you wish to have long press with `Button` is to write your own press and long press gesture, Modifier.combinedClickable is detectTrapGestures under the hood with PointerEventPass.Initial. I posted similar answer for transform gestures here. https://stackoverflow.com/questions/75955466/jetpack-compose-intercept-pinch-zoom-in-child-layout – Thracian Jun 03 '23 at 10:22
  • I've tried to use Box and Sufrace, but interactionSource seems not working with it. I need to have an access to press state in order to change some colours. – kkkkk Jun 03 '23 at 11:22
0

you can try this

 modifier = Modifier
           .weight(2f)
           .pointerInput(Unit){
               detectTapGestures(
                     onLongPress = {
                             // perform some action here..
                     }
               )
            }
zaid khan
  • 825
  • 3
  • 11