This code snippet demonstrates how to implement a double click listener in Jetpack Compose.
Here's a breakdown of the code:
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.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.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.Box: The
Box
function creates a container that can hold other composables. It can be sized and positioned using modifiers.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".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.
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
)
}
}
}
}
- jetpack compose - Background brush
- jetpack compose - Combined clickable
- jetpack compose - Long click listener
- jetpack compose - Pass onClick event to function
- jetpack compose - Kotlinx serialization handle null values
- jetpack compose - Kotlinx serialization not encode null values
- jetpack compose - Kotlinx serialization encode to string
- jetpack compose - Kotlinx serialization allow special floating point values
- jetpack compose - Kotlinx serialization parse to json element
- jetpack compose - Kotlinx serialization build json element
- jetpack compose - Kotlinx serialization decode josn element
- jetpack compose - How to use kotlin flow
- jetpack compose - Random number flow
- jetpack compose - Icon from vector resource
- jetpack compose - IconButton from vector resource