Skip to content
Related Articles

Related Articles

Improve Article
Count the Number of Taps (Multi-Tapping) on a View in Android
  • Last Updated : 18 Feb, 2021

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- 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/tv1"
        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 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.

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()
                          
                          
                        // Handle Multiple Taps on the View////////////////////////////////
                       handler.postDelayed(Runnable {
                           
                         // Toast the number of Taps of current User Event
                                Toast.makeText(applicationContext, "$numberOfTaps Clicks"
                                               Toast.LENGTH_SHORT)
                                        .show()
                            }, ViewConfiguration.getDoubleTapTimeout().toLong())
                     /////////////////////////////////////////////////////////////////////
                      
                    }
                }
                return true
            }
        })
    }
}

 

Output on the Emulator:

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 :