Jetpack compose: How to implement double tap listener

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:

  1. 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 the isLighter value.
  2. 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 a pointerInput modifier. This modifier allows us to detect pointer interactions like taps.
  3. Double Tap Detection: Inside the pointerInput modifier, the detectTapGestures 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, the isLighter value is inverted through negation (!).
  4. 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.
  5. 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)

  6. 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.


MainActivity.kt

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()
    }
}
More android jetpack compose tutorials