Jetpack Compose - Double Tap Listener
This code demonstrates how to implement a double tap listener in Jetpack Compose. It creates a clickable text element that changes color when double-tapped.
Breakdown:
State Management: The code uses two state variables:
isLighter
: A mutableStateOf boolean variable that tracks whether the text should be displayed in a lighter or darker color.color
: An animated color state that interpolates between light and dark colors based on theisLighter
value.
MainContent Composable: The
MainContent
composable defines the layout and behavior of the screen.- It uses a
Column
to center the content vertically and horizontally. - It creates a
Text
composable with the text "Double Tap Me". - The
Text
composable is wrapped with apointerInput
modifier. This modifier allows us to detect pointer interactions like taps.
- It uses a
Double Tap Detection: Inside the
pointerInput
modifier, thedetectTapGestures
function is used. This function listens for tap gestures on the text element.- The
onDoubleTap
callback is triggered when the user double-taps the text. Inside this callback, theisLighter
value is inverted through negation (!
).
- The
Color Change Animation: The
animateColorAsState
function is used to create an animated color state variable.- It takes the target color as a parameter, which is dynamically set based on the
isLighter
value. - This animation ensures a smooth transition between the light and dark colors when the text is double-tapped.
- It takes the target color as a parameter, which is dynamically set based on the
Text Color Binding: The
color
variable from the animated color state is used to set the color of the text. This ensures the text reflects the current state (lighter or darker)Preview: The
ComposablePreview
function is included for previewing the composable in the Android Studio preview window. However, the functionality of the double-tap listener cannot be tested within the preview.
Summary
This code showcases a simple way to implement a double tap listener in Jetpack Compose. It leverages state management, pointer input handling, and color animation to create an interactive element that responds to user interaction.
package com.cfsuman.jetpackcompose
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.compose.animation.animateColorAsState
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.*
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MainContent()
}
}
@Composable
fun MainContent(){
Column(
modifier = Modifier
.background(Color(0xFFEDEAE0))
.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
val isLighter = remember { mutableStateOf(true) }
val color = animateColorAsState(
targetValue = if (isLighter.value)
Color(0xFFB2BEB5) else Color(0xFFFF0800)
)
Text(
text = "Double Tap Me",
fontSize = 50.sp,
color = color.value,
modifier = Modifier
.padding(25.dp)
.pointerInput(Unit) {
detectTapGestures(
onDoubleTap = {
isLighter.value = !isLighter.value
}
)
}
)
}
}
@Preview
@Composable
fun ComposablePreview(){
//MainContent()
}
}
- jetpack compose - AnimateColorAsState duration
- jetpack compose - AnimateDpAsState
- jetpack compose - Infinite float animation
- jetpack compose - Animation start delay
- jetpack compose - Rotate animation
- jetpack compose - Draw circle on canvas
- jetpack compose - Draw rectangle on canvas
- jetpack compose - Draw rounded rectangle on canvas
- jetpack compose - Canvas withTransform
- jetpack compose - Canvas inset
- jetpack compose - Draw arc on canvas
- jetpack compose - Long press listener
- jetpack compose - Tap listener
- jetpack compose - Press listener
- jetpack compose - Column vertical scrolling