Open In App

Triple-Tap on a View Using onTouchListener Object in Android

Last Updated : 18 Feb, 2021
Like Article

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- 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 version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""
        android:text="Click Here"

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.


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
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        // Declaring Text View from the Layout file
        val tvv = findViewById<TextView>(
        // 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
                        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"
                            // Double tap
                        } else if (numberOfTaps == 2) {
                            handler.postDelayed(Runnable {
                                Toast.makeText(applicationContext, "Double-Click"
                            }, ViewConfiguration.getDoubleTapTimeout().toLong())
                return true


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads