Jetpack Compose - Long Press Listener
This code example demonstrates how to implement a long press listener in Jetpack Compose. It creates a text element that rotates when the user long presses on it.
Breakdown:
Setting Up:
- The code defines a
MainActivity
class that extendsAppCompatActivity
. - Inside
onCreate
, it sets the content usingsetContent
with theMainContent
composable.
- The code defines a
Main Content:
MainContent
uses aColumn
to arrange its children vertically and centered.- It uses
remember
to create a mutable state variableisRotated
to track the rotation state of the text. animateFloatAsState
animates theangle
value based on theisRotated
state, with a duration of 3 seconds and a specific easing function.
Text with Long Press Detection:
- A
Text
composable displays the text "Long Press Me". - The
Modifier
applies padding, rotates the text based on theangle
animation, and usespointerInput
to capture user interactions. - Inside
pointerInput
,detectTapGestures
is used to detect long presses. - When a long press is detected (
onLongPress
), theisRotated
state is toggled, triggering the rotation animation.
- A
Preview:
- The
ComposablePreview
function is for previewing the composable in the Android Studio toolset. - It's currently commented out, but you can uncomment
MainContent
to see the preview.
- The
Summary
This example showcases a basic implementation of long press detection in Jetpack Compose. It utilizes state variables, animations, and the pointerInput
modifier with detectTapGestures
to achieve the desired functionality. This approach offers a clean and declarative way to handle long press interactions within your composables.
MainActivity.kt
package com.cfsuman.jetpackcompose
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.compose.animation.core.FastOutSlowInEasing
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.tween
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.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.rotate
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 isRotated = remember { mutableStateOf(false) }
val angle: Float by animateFloatAsState(
targetValue = if (isRotated.value) 360F else 0F,
animationSpec = tween(
durationMillis = 3000, // duration
easing = FastOutSlowInEasing
)
)
Text(
text = "Long Press Me",
fontSize = 50.sp,
color = Color(0xFF7C0A02),
modifier = Modifier
.padding(25.dp)
.rotate(angle)
.pointerInput(Unit) {
detectTapGestures(
onLongPress = {
isRotated.value = !isRotated.value
}
)
}
)
}
}
@Preview
@Composable
fun ComposablePreview(){
//MainContent()
}
}
- jetpack compose - Double tap listener
- jetpack compose - Tap listener
- jetpack compose - Press listener
- jetpack compose - Column vertical scrolling
- jetpack compose - Swiping
- jetpack compose - Panning zooming rotating
- jetpack compose - Weight modifier
- jetpack compose - Button with icon
- jetpack compose - Button color
- jetpack compose - Button rounded corners
- jetpack compose - TextField remove underline
- jetpack compose - TextField background color
- jetpack compose - TextField hint
- jetpack compose - TextField clear focus
- jetpack compose - TextField focus change listener