Button Long Press Listener in Android jetpack compose

SoftwareGuy

I am having a Android Composable UI with a Button. How can I track Button Long Press events? I got it working for "Text" Long press, but for Button It is not working. Same way like below if I apply modifier to button, it is not working.

Text(
                        text = view.text,
                        fontSize = view.textFontSize.toInt().sp,
                        fontWeight = FontWeight(view.textFontWeight.toInt()),
                        color = Color(android.graphics.Color.parseColor(view.textColor)),
                        modifier = Modifier.clickable(onClick = {
                                println("Single Click")
                            }, onLongClick = {
                                println("Long Click")
                            }, onDoubleClick = {
                                println("Double Tap")
                     })
                ) 
adneal

The best way to handle this is to roll your own Button. The Material Button is basically just a Surface and a Row. The reason adding your own Modifier.clickable doesn't work is because one is already set.

So, if you'd like to add onLongPress, etc you can copy/paste the default implementation and pass those lambdas in.

@Composable
@OptIn(ExperimentalMaterialApi::class)
fun Button(
    onClick: () -> Unit,
    modifier: Modifier = Modifier,
    onLongClick: (() -> Unit)? = null,
    onDoubleClick: (() -> Unit)? = null,
    enabled: Boolean = true,
    interactionState: InteractionState = remember { InteractionState() },
    elevation: ButtonElevation? = ButtonDefaults.elevation(),
    shape: Shape = MaterialTheme.shapes.small,
    border: BorderStroke? = null,
    colors: ButtonColors = ButtonDefaults.buttonColors(),
    contentPadding: PaddingValues = ButtonDefaults.ContentPadding,
    content: @Composable RowScope.() -> Unit
) {
    val contentColor = colors.contentColor(enabled)
    Surface(
        shape = shape,
        color = colors.backgroundColor(enabled),
        contentColor = contentColor.copy(alpha = 1f),
        border = border,
        elevation = elevation?.elevation(enabled, interactionState) ?: 0.dp,
        modifier = modifier.clickable(
            onClick = onClick,
            onLongClick = onLongClick,
            onDoubleClick = onDoubleClick,
            enabled = enabled,
            role = Role.Button,
            interactionState = interactionState,
            indication = null
        )
    ) {
        Providers(AmbientContentAlpha provides contentColor.alpha) {
            ProvideTextStyle(value = MaterialTheme.typography.button) {
                Row(
                    Modifier
                        .defaultMinSizeConstraints(
                            minWidth = ButtonDefaults.MinWidth,
                            minHeight = ButtonDefaults.MinHeight
                        )
                        .indication(interactionState, AmbientIndication.current())
                        .padding(contentPadding),
                    horizontalArrangement = Arrangement.Center,
                    verticalAlignment = Alignment.CenterVertically,
                    content = content
                )
            }
        }
    }
}

Usage:

Button(
    onClick = {},
    onLongClick = {},
    onDoubleClick = {}
) {
    Text(text = "I'm a button")
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Android Jetpack Compose by rememberSaveable State does not survive Back Button

From Dev

Is long press natural for Android?

From Dev

Is it possible to add long press listener to CheckBoxPreference?

From Dev

Detect power button long press

From Dev

Button tap and long press gesture

From Dev

Volume Button long press event

From Java

Android Jetpack compose IconButton padding

From Dev

Android long click listener

From Dev

Mousedown event listener for button press not working

From Dev

AS3 | Event Listener for press Delete button

From Dev

Android Multiple Button Press

From Dev

Notification Long Press Options Android

From Dev

Android Crash on EditText long press

From Dev

Notification Long Press Options Android

From Dev

Long press on Android menu item?

From Dev

Android button onLongClick, how to get the listener when the button is released from a long click

From Dev

Android button onLongClick, how to get the listener when the button is released from a long click

From Java

Disable Long Press back Button (callout menu)

From Dev

Deleting the text in UITextFiled when long press on a button

From Dev

Count seconds on button long press in javascript

From Dev

Camera button long press event in windows tablet

From Dev

Android - Launch activity from search button - remove my app's activity from search long press options

From Dev

how to enable GPS on long key press of power button while screen is locked in Android?

From Dev

how to enable GPS on long key press of power button while screen is locked in Android?

From Dev

How to long press on android record button(during chat) for a particular time duration in appium test, by java

From Java

Jetpack compose: Android Tests won't run

From Dev

How to add Image in Android using Jetpack Compose

From Dev

Zoomout animation on button press in android

From Dev

Android: Press Done Button on Keyboard

Related Related

  1. 1

    Android Jetpack Compose by rememberSaveable State does not survive Back Button

  2. 2

    Is long press natural for Android?

  3. 3

    Is it possible to add long press listener to CheckBoxPreference?

  4. 4

    Detect power button long press

  5. 5

    Button tap and long press gesture

  6. 6

    Volume Button long press event

  7. 7

    Android Jetpack compose IconButton padding

  8. 8

    Android long click listener

  9. 9

    Mousedown event listener for button press not working

  10. 10

    AS3 | Event Listener for press Delete button

  11. 11

    Android Multiple Button Press

  12. 12

    Notification Long Press Options Android

  13. 13

    Android Crash on EditText long press

  14. 14

    Notification Long Press Options Android

  15. 15

    Long press on Android menu item?

  16. 16

    Android button onLongClick, how to get the listener when the button is released from a long click

  17. 17

    Android button onLongClick, how to get the listener when the button is released from a long click

  18. 18

    Disable Long Press back Button (callout menu)

  19. 19

    Deleting the text in UITextFiled when long press on a button

  20. 20

    Count seconds on button long press in javascript

  21. 21

    Camera button long press event in windows tablet

  22. 22

    Android - Launch activity from search button - remove my app's activity from search long press options

  23. 23

    how to enable GPS on long key press of power button while screen is locked in Android?

  24. 24

    how to enable GPS on long key press of power button while screen is locked in Android?

  25. 25

    How to long press on android record button(during chat) for a particular time duration in appium test, by java

  26. 26

    Jetpack compose: Android Tests won't run

  27. 27

    How to add Image in Android using Jetpack Compose

  28. 28

    Zoomout animation on button press in android

  29. 29

    Android: Press Done Button on Keyboard

HotTag

Archive