Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button Long Press Listener in Android jetpack compose

I am having an Android Composable UI with a Button.

How can I track button long press events? I got it working for the Text long press, but for Button, It is not working. Same way like below if I apply a modifier to the 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")
        },
    ),
)
like image 567
SoftwareGuy Avatar asked Jan 21 '21 21:01

SoftwareGuy


People also ask

How can we call a @composable function inside an onclick ()?

You can't call set a Composable inside your non Composable scope. What you should do is have a State with boolean and set it to true when you want to show your composable. This is also how we display dialogs or conditional Composable such as Loading, Result, Error.

How do I make my screen scrollable in jetpack compose?

Add Modifier. scrollable(..) to the container that you wish to make scrollable. Code would be something like this: val scrollState = rememberScrollState() ... Box( // or whatever your parent composable is modifier = Modifier .

Is jetpack easier to compose?

Jetpack Compose is Android's modern toolkit for building native UI. It simplifies and accelerates UI development on Android bringing your apps to life with less code, powerful tools, and intuitive Kotlin APIs. It makes building Android UI faster and easier.

What is a button in Android with Jetpack?

A Button is a UI component in Android which is used to navigate between different screens. With the help of a button, the user can interact with your app and perform multiple actions inside your application. In this article, we will take a look at the implementation of buttons in Android using Jetpack compose.

How to implement navigation between screens in Android apps using jetpack compose?

The Navigation component might help us in implementing the navigation between screens in Android apps. There is also a dedicated navigation compose dependency that supports UI declared in Jetpack Compose toolkit. We just need to create the NavHost, pass the NavController instance and define composable destinations:

Why can't I close the bottom sheet in jetpack compose?

The problem comes when you want to perform a custom action on the user back press or when you want to close the bottom sheet in compose. As Jetpack compose bottom sheets are not backed by navigation library, system back press closes the current screen instead of the bottom sheet.

What are the different types of buttons in jetpack compose?

In general, there are three types of Buttons provided by Material Design Component library for Jetpack Compose. 1. Button (): A normal and regular button 2. OutlinedButton (): Transparent background but with an outline 3. TextButton (): A normal button that appears like a text


2 Answers

You can use combinedClickable like the following:

Modifier
    .combinedClickable(
        onClick = { },
        onLongClick = { },
    )

Warning: with Compose 1.0.1 this method is marked as @ExperimentalFoundationApi so this answer may get outdated in the future releases.

like image 176
Piotr Aleksander Chmielowski Avatar answered Oct 16 '22 13:10

Piotr Aleksander Chmielowski


https://developer.android.com/jetpack/compose/gestures can be used as well.

for example:

 import androidx.compose.ui.input.pointer.pointerInput
 import androidx.compose.foundation.gestures.detectTapGestures

 modifier = Modifier
           .weight(2f)
           .pointerInput(Unit){
               detectTapGestures(
                     onLongPress = {
                             // perform some action here..
                     }
               )
            }
                        
like image 19
Santosh Pillai Avatar answered Oct 16 '22 11:10

Santosh Pillai