Open In App

Detect Different Types of Touch Gestures in Android using Jetpack Compose

Improve
Improve
Like Article
Like
Save
Share
Report

Most of the smartphones of this generation have a touchscreen which serves as a medium for input as well as output. Users can click the screen for giving inputs to the device and the same screen is used for displaying the output for the given action. A user may touch different points on the screen as well as draw gestures, it is important for a developer to figure out various touch gestures. So in this article, we will show you how you could detect different types of Screen Touch Gestures in Android using Jetpack Compose. Follow the below steps once the IDE is ready.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. While choosing the template, select Empty Compose Activity. If you do not find this template, try upgrading the Android Studio to the latest version. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.

Kotlin




package com.geeksforgeeks.jcdetectscreentouch
  
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
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.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
  
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
  
            // Creating a Simple Scaffold 
            // Layout for the application
            Scaffold(
  
                // Creating a Top Bar
                topBar = { TopAppBar(title = { Text("GFG | Detect Touch Gestures", color = Color.White) }, backgroundColor = Color(0xff0f9d58)) },
  
                // Creating Content
                content = {
  
                    val mContext = LocalContext.current
  
                    // Creating a Column Layout
                    Column(Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center) {
  
                        // Creating a Column to detect Press
                        Column(Modifier.fillMaxWidth().height(150.dp).background(Color.Black).pointerInput(key1 = "someStringKey?") {
                                    detectTapGestures(
                                        onPress = { Toast.makeText(mContext, "Press Detected", Toast.LENGTH_SHORT).show() }
                                    )
                                }){
                            Text("OnPress", fontSize = 20.sp, color = Color.White)
                        }
  
                        // Adding a Space of height 50dp
                        Spacer(modifier = Modifier.height(50.dp))
  
                        // Creating a Column to detect Long Press
                        Column(Modifier.fillMaxWidth().height(150.dp).background(Color.Black).pointerInput(key1 = "someStringKey?") {
                            detectTapGestures(
                                onLongPress = { Toast.makeText(mContext, "Long Press Detected", Toast.LENGTH_SHORT).show() }
                            )
                        }){
                            Text("OnLongPress", fontSize = 20.sp, color = Color.Green)
                        }
  
                        // Adding a Space of height 50dp
                        Spacer(modifier = Modifier.height(50.dp))
  
                        // Creating a Column to detect Tap (when finger press is released)
                        Column(Modifier.fillMaxWidth().height(150.dp).background(Color.Black).pointerInput(key1 = "someStringKey?") {
                            detectTapGestures(
                                onTap = { Toast.makeText(mContext, "Tap Detected", Toast.LENGTH_SHORT).show() }
                            )
                        }){
                            Text("OnTap", fontSize = 20.sp, color = Color.Yellow)
                        }
  
                        // Adding a Space of height 50dp
                        Spacer(modifier = Modifier.height(50.dp))
  
                        // Creating a Column to detect Double Tap
                        Column(Modifier.fillMaxWidth().height(150.dp).background(Color.Black).pointerInput(key1 = "someStringKey?") {
                            detectTapGestures(
                                onDoubleTap = { Toast.makeText(mContext, "Double Tap Detected", Toast.LENGTH_SHORT).show() }
                            )
                        }){
                            Text("OnDoubleTap", fontSize = 20.sp, color = Color.Red)
                        }
  
                    }
                }
            )
        }
    }
}


Output:

In the output, you can see that we are able to detect various types of touch gestures.



Last Updated : 26 Apr, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads