Jetpack compose: How to implement double click listener

This code snippet demonstrates how to implement a double click listener in Jetpack Compose.

Here's a breakdown of the code:

  1. Scaffold: The Scaffold function is the foundation of the user interface in Jetpack Compose. It provides a basic structure for the screen, including an optional top app bar, content area, and bottom navigation bar.

  2. TopAppBar: The TopAppBar composes the app bar at the top of the screen. It displays the app title ("Compose - Double click listener" in this case) and can optionally include navigation icons or other elements.

  3. MainContent: The MainContent function defines the content of the screen. In this example, it's a clickable box that changes its shape and displays a message based on the user's interaction.

  4. Box: The Box function creates a container that can hold other composables. It can be sized and positioned using modifiers.

  5. combinedClickable: The combinedClickable modifier allows you to attach both click and double-click listeners to a composable. In this case, the box changes its shape to a circle on a double click and displays the message "Double Clicked". When clicked once, it reverts to its original rounded corner shape and displays the message "Clicked".

  6. Text: The Text function displays text on the screen. The text displayed here updates based on the user's interaction with the box.

In summary, this code shows how to create a clickable element that responds differently to single and double clicks using Jetpack Compose. This can be useful for implementing features like zooming in on an image with a double click.


MainActivity.kt

package com.cfsuman.jetpackcompose

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.compose.foundation.*
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.graphics.Color
import androidx.compose.material.Text
import androidx.compose.material.TopAppBar
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.unit.dp


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedObjectState: Bundle?) {
        super.onCreate(savedObjectState)
        setContent {
            GetScaffold()
        }
    }


    @Composable
    fun GetScaffold() {
        Scaffold(
            topBar = {
                TopAppBar(
                    title = {
                        Text(
                            text = "Compose - Double click listener"
                        )
                    },
                    backgroundColor = Color(0xFF1ca9c9),
                )
            },
            content = { MainContent() },
            backgroundColor = Color(0xFFF3ebad)
        )
    }


    @OptIn(ExperimentalFoundationApi::class)
    @Composable
    fun MainContent() {
        val message = remember { mutableStateOf("Click Action")}
        val shape = remember { mutableStateOf(RoundedCornerShape(24.dp))}

        Column(
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.Center,
            modifier = Modifier.fillMaxSize().padding(12.dp)
        ) {
            Box(
                modifier = Modifier
                    .size(250.dp)
                    .clip(shape.value)
                    .background(SolidColor(Color(0xFFE1ae84)))
                    .combinedClickable(
                        enabled = true,
                        onClick = {
                          shape.value = RoundedCornerShape(24.dp)
                            message.value = "Clicked"
                        },
                        onDoubleClick = {
                            shape.value = CircleShape
                            message.value = "Double Clicked"
                        }
                    )
                    .padding(12.dp),
                contentAlignment = Alignment.Center
            ) {
                Text(
                    text = message.value,
                    style = MaterialTheme.typography.h5
                )
            }
        }
    }
}
More android jetpack compose tutorials