Skip to content
Related Articles

Related Articles

Triple-Tap on a View Using onTouchListener Object in Android
  • Last Updated : 18 Feb, 2021

To detect a triple tap i.e. whenever the user triple taps on any view how it is detected and according to the view a response can be added corresponding to it. Here an example is shown in which the user triple taps on the view, it is detected and corresponding to the view response is added in the form of toast.

Step 1: Create an Empty activity in Android Studio. To create one, follow this article- https://www.geeksforgeeks.org/android-how-to-create-start-a-new-project-in-android-studio/. Check if the primary language selected is Kotlin.

Step 2: No change is done in activity_main.xml. Since already a textview is present so the response for the triple tap is added with it.

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Click Here"
        />
  
</RelativeLayout>

Step 3: In this step add the onTouchListenter with the view. Here in the listener initialize a variable to 1 which will keep the count of taps user has done. If the tap counts in a short time becomes 3 then the response corresponding to the view is initiated. Below is the code for the MainActivity.kt class.

Kotlin




package org.geeksforgeeks.tripletap
  
import android.os.Bundle
import android.os.Handler
import android.view.MotionEvent
import android.view.View
import android.view.ViewConfiguration
import android.widget.Button
import android.widget.RelativeLayout
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
  
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // Declaring Text View from the Layout file
        val tvv = findViewById<TextView>(R.id.tv)
  
        // Implementing onTouchListener on our Text View
        tvv.setOnTouchListener(object : View.OnTouchListener {
          // Handler to handle the number of clicks
            var handler: Handler = Handler()
            var numberOfTaps = 0
            var lastTapTimeMs: Long = 0
            var touchDownMs: Long = 0
            override fun onTouch(v: View?, event: MotionEvent): Boolean {
                when (event.action) {
                    MotionEvent.ACTION_DOWN -> touchDownMs = System.currentTimeMillis()
                    MotionEvent.ACTION_UP -> {
                      // Handle the numberOfTaps
                        handler.removeCallbacksAndMessages(null)
                        if (System.currentTimeMillis() - touchDownMs 
                            > ViewConfiguration.getTapTimeout()) {
                            //it was not a tap
                            numberOfTaps = 0
                            lastTapTimeMs = 0
                        }
                        if (numberOfTaps > 0
                            && System.currentTimeMillis() - lastTapTimeMs 
                            < ViewConfiguration.getDoubleTapTimeout()
                        ) {
                          // if the view was clicked once
                            numberOfTaps += 1
                        } else {
                          // if the view was never clicked
                            numberOfTaps = 1
                        }
                        lastTapTimeMs = System.currentTimeMillis()
                          
                        // Triple Tap
                        if (numberOfTaps == 3) {
                            Toast.makeText(applicationContext, "Triple-Click"
                                           Toast.LENGTH_SHORT).show()
                            // Double tap
                        } else if (numberOfTaps == 2) {
                            handler.postDelayed(Runnable {
                                Toast.makeText(applicationContext, "Double-Click"
                                               Toast.LENGTH_SHORT)
                                    .show()
                            }, ViewConfiguration.getDoubleTapTimeout().toLong())
                        }
                    }
                }
                return true
            }
        })
    }
}

Output:

Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!



My Personal Notes arrow_drop_up
Recommended Articles
Page :