How do you get a `Button` to respond to both a pre...
# compose
r
How do you get a
Button
to respond to both a press and a long press? I tried modifier
.pointerInput
with
detectTapGestures
and
onLongPress
but it does not detect the long press (or any of the other gestures). If I include a
Text
in the Button I can get the long press to work on the text portion, but I want to cover the whole button. (See code in thread.) Edit: I can't get
Modifier.combinedClickable
to do it either (see second code example in thread).
1
Copy code
@Composable
fun LongPressButtonTest() {
    Button(
        modifier = Modifier.size(100.dp).pointerInput(Unit) {
            detectTapGestures(
                onTap = { println("(Button onTap)") },
                onDoubleTap = { println("(Button onDoubleTap)") },
                onLongPress = { println("(Button onLongPress)") }
            )
        },
        onClick = { println("(Button onClick)")  }
    ) {
        Text(
            text = "Text",
            color = Color.White,
            modifier = Modifier.pointerInput(Unit) {
                detectTapGestures(
                    onTap = { println("(Text onTap)") },
                    onDoubleTap = { println("(Text onDoubleTap)") },
                    onLongPress = { println("(Text onLongPress)") }
                )
            }
        )
    }
}
I tried
@ExperimentalFoundationApi
Modifier.combinedClickable
and I can't get that to work either (what am I missing?):
Copy code
@ExperimentalFoundationApi
@Composable
fun CombinedClickableButtonTest() {
    Button(
        modifier = Modifier.size(100.dp)
            .combinedClickable(
                onLongClick = { println("(Button modifier onLongClick)") },
                onDoubleClick = { println("(Button modifier onDoubleClick)") },
                onClick = { println("(Button modifier onClick)") }
            ),
        onClick = { println("(Button onClick)")  }
    ) {
        Text(
            text = "Text",
            color = Color.White,
        )
    }
}
d
I think you need to get rid of Button and apply combinedClickable on something else
r
Wow, I wasn't expecting a button to not be "long clickable" (think delete key on a calculator that is both delete and clear). The "something else" in my case would be
Text
and
Icon
. I suppose I could get button behaviors like elevation and indication through modifiers, but how would I get
enabled
?
d
@Rick Regan, just look into the Button's source. You can copy it and modify however you like and have your own LongClickableButton :)
r
Thanks. I see that suggested a lot here but I've been reluctant to do that. I don't know if I should be calling functions like
CompositionLocalProvider
and
ProvideTextStyle
, and I also think about having to merge changes that may occur every two weeks with each release.