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")
},
),
)
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.
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 .
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.
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.
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:
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.
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
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.
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..
}
)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With