Home > OS >  How to detect press gesture on Column (Jetpack Compose)?
How to detect press gesture on Column (Jetpack Compose)?

Time:10-17

I'm trying to detect a press gesture on a Column in Jetpack Compose.

Column(
    modifier = Modifier
        .pointerInput(Unit) {
            detectTapGestures(
                onPress = {
                    Log.d("OnPress", "I've been pressed")
                },
                onTap =  {
                    Log.d("OnPress", "I've been tapped")
                },
                onDoubleTap = {
                    Log.d("OnPress", "Got double tapped")
                },
                onLongPress = {
                    Log.d("OnPress", "Long pressed")
                }
            )
        }
)

However, none of the logs are working.

I've seen InteractionSource having a way to collect information about whether a component is pressed/focused etc. https://developer.android.com/jetpack/compose/handling-interaction#interaction-state

But I can't give my Column an interaction source as a parameter.

So how do I get press detected on my Column?

CodePudding user response:

I'm not sure about your use-case, but when I tried your code, and though I specify a mock size for the Column, its working

Column(
        modifier = Modifier
            .size(400.dp) // mock size
            .background(Color.Red)
            .pointerInput(Unit) {
                detectTapGestures(
                    onPress = {
                        Log.e("OnPress", "I've been pressed")
                    },
                    onTap =  {
                        Log.e("OnPress", "I've been tapped")
                    },
                    onDoubleTap = {
                        Log.e("OnPress", "Got double tapped")
                    },
                    onLongPress = {
                        Log.e("OnPress", "Long pressed")
                    }
                )
            }
    ) {}

prints

I've been pressed
I've been tapped
I've been pressed
I've been tapped

CodePudding user response:

If both Modifiers, clickable and pointerInput are set on the same Composable, the later one in the Modifier chain will be used.

    modifier = Modifier
        .pointerInput(Unit) {
            //
        }
        .clickable { Log.d("OnClick", "Click") }

If clickable() comes after pointerInput, all of gesture onPress, onTap,onDoubleTap and onLongPress will be ignored and clickable() will be invoked instead.

As result:

D/OnClick: Click

Instead:

    modifier = Modifier
        .clickable { Log.d("OnClick", "Click") }
        .pointerInput(Unit){
            //your code
        } 

as result:

E/OnPress: I've been pressed
E/OnPress: I've been tapped

You can also use the Modifier.combinedClickable() to listen for single, double, and long clicks.

   Column(
        modifier = Modifier
            .combinedClickable(
                onLongClick =  { Log.d("OnClick", "Long click") },
                onDoubleClick = { Log.d("OnClick", "Double click")},
                onClick = {  Log.d("OnClick", "Click")}
            )
   )
  • Related