Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

Count the Number of Taps (Multi-Tapping) on a View in Android

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

In this article, the number of taps that user does in a  short span of time on a particular view is counted, It can be used for adding different response with different taps. For example one tap on the word will show the meaning of the word while double tap will show its synonyms in a dictionary app. Below is an example in which the number of taps done in textview is shown as a 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 0 which will keep the count of taps user has done. Every time user taps in a short span of time the variable value is increased by one. 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()
                        // Handle Multiple Taps on the View////////////////////////////////
                       handler.postDelayed(Runnable {
                         // Toast the number of Taps of current User Event
                                Toast.makeText(applicationContext, "$numberOfTaps Clicks"
                            }, ViewConfiguration.getDoubleTapTimeout().toLong())
                return true


Output on the Emulator:

My Personal Notes arrow_drop_up
Last Updated : 18 Feb, 2021
Like Article
Save Article
Similar Reads