I've been stuck on this for a while. I have two "buttons" that performs two actions: it adds/removes +-1 on a single click and adds/removes +-10 on a long click. I want to modify it so that it works like this:
When the user taps (clicks) the button, it adds/removes +-1. When the user taps and holds, it should add/remove +-10 every 0.5 seconds until the user releases the button.
How can i solve this in the best way? Best / F
Here is my current code:
Box(
modifier = Modifier
.fillMaxSize()
.background(Color.Transparent)
.weight(2f)
binedClickable(
onClick = {
when (selectedCounter) {
"Life" -> if (life > 0) life -= 1
"Poison" -> if (poison > 0) poison -= 1
}
consecutiveTapsRemove = maxOf(0, consecutiveTapsRemove + 1)
},
onLongClick = {
when (selectedCounter) {
"Life" -> if (life > 0) life -= 10
"Poison" -> if (poison > 0) poison -= 10
}
consecutiveTapsRemove = maxOf(0, consecutiveTapsRemove + 10)
}
),
) {}
Box(
modifier = Modifier
.fillMaxSize()
.background(Color.Transparent)
.weight(2f)
binedClickable(
onClick = {
when (selectedCounter) {
"Life" -> life += 1
"Poison" -> poison += 1
}
consecutiveTapsAdd = maxOf(0, consecutiveTapsAdd + 1)
},
onLongClick = {
when (selectedCounter) {
"Life" -> life += 10
"Poison" -> poison += 10
}
consecutiveTapsAdd = maxOf(0, consecutiveTapsAdd + 10)
}
)
) {}
I've been stuck on this for a while. I have two "buttons" that performs two actions: it adds/removes +-1 on a single click and adds/removes +-10 on a long click. I want to modify it so that it works like this:
When the user taps (clicks) the button, it adds/removes +-1. When the user taps and holds, it should add/remove +-10 every 0.5 seconds until the user releases the button.
How can i solve this in the best way? Best / F
Here is my current code:
Box(
modifier = Modifier
.fillMaxSize()
.background(Color.Transparent)
.weight(2f)
binedClickable(
onClick = {
when (selectedCounter) {
"Life" -> if (life > 0) life -= 1
"Poison" -> if (poison > 0) poison -= 1
}
consecutiveTapsRemove = maxOf(0, consecutiveTapsRemove + 1)
},
onLongClick = {
when (selectedCounter) {
"Life" -> if (life > 0) life -= 10
"Poison" -> if (poison > 0) poison -= 10
}
consecutiveTapsRemove = maxOf(0, consecutiveTapsRemove + 10)
}
),
) {}
Box(
modifier = Modifier
.fillMaxSize()
.background(Color.Transparent)
.weight(2f)
binedClickable(
onClick = {
when (selectedCounter) {
"Life" -> life += 1
"Poison" -> poison += 1
}
consecutiveTapsAdd = maxOf(0, consecutiveTapsAdd + 1)
},
onLongClick = {
when (selectedCounter) {
"Life" -> life += 10
"Poison" -> poison += 10
}
consecutiveTapsAdd = maxOf(0, consecutiveTapsAdd + 10)
}
)
) {}
Share
Improve this question
asked Feb 15 at 10:48
BjurhagerBjurhager
554 bronze badges
2
- You should add a timer task that executes every 5 seconds. Meanwhile, if the user releases the button, cancel the task. – Rajen Raiyarela Commented Feb 15 at 11:21
- this article may be helpfully. link: proandroiddev/… – Meet Commented Feb 15 at 15:12
1 Answer
Reset to default 0Solved it, Here is how i made it work.
var testAction by remember { mutableStateOf("none") }
var testCount by remember { mutableIntStateOf(0) }
Box(
modifier = Modifier
.padding(20.dp)
.pointerInput(Unit) {
detectTapGestures(
onPress = {
val released = try {
tryAwaitRelease()
} catch (c: CancellationException) {
false
}
if (released) {
testAction = "Released"
// Stop Timer
} else {
testAction = "Canceled"
}
},
onTap = {
testCount += 1
},
onLongPress = {
testAction = "Long press"
// Start timer
}
)
}
) {
Column {
Text(testCount.toString()) // Display the tap count
Text(testAction) // Display the action text
Box(
modifier = Modifier
.background(Color.Red)
.padding(20.dp)
) {
Text("Hold Test")
}
}
}