0

I have a toggle button. It works fine, but I want to be able to long-press it.

      IconToggleButton(
            checked = isChecked,
            onCheckedChange = onCheck,
            modifier = modifier
                .pointerInput(Unit) {
                    detectTapGestures(
                        onLongPress = {
                            showingHelpDialog = true
                        }
                    )
                } 
        ) {
            Icon(
                imageVector = if (isChecked) {
                    Icons.Filled.Star
                } else {
                    Icons.Filled.StarBorder
                },
                contentDescription = null,
            )
        }

This doesn't work, because IconToggleButton itself overrides pointerInput through the toggleable modifier.

I could copy all the source code from IconToggleButton/toggleable and hack in my own pointerInput. But that would require me to copy a lot of low-level component logic which I expressly don't want to copy into my own source code, because it should be part of the Material library and I want to benefit from potential improvements in the Material library.

Is there another way I could listen for long-press events?

Maarten
  • 6,894
  • 7
  • 55
  • 90

1 Answers1

0

instead of using

modifier.pointerInput(Unit) { /*...*/ }

use

modifier.combinedClickable(
        onLongClick = { /*....*/ },
        onClick ={ /*....*/ })
Koch
  • 555
  • 4
  • 15