Open In App
Related Articles

Double-Tap on a View in Android

Improve Article
Save Article
Like Article

Detecting a double tap i.e. whenever the user double 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 double tap on the view is detected and corresponding to it a 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 double tap is added with it.


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android=""
        android:text="Hello World!"
        app:layout_constraintTop_toTopOf="parent" />

Step 3: In this step add the abstract class for the double tap and set the onClickListener which will use the abstract class. Below is the code for the MainActivity.kt class.


package org.geeksforgeeks.viewdoubletap
import android.os.Bundle
import android.view.View
import android.widget.TextView
import android.widget.Toast
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        // In our case, we tap on Text View
        val view = findViewById<TextView>(
        // Double Click Listener implemented on the Text View
        view.setOnClickListener(object : DoubleClickListener() {
            override fun onDoubleClick(v: View?) {
                Toast.makeText(applicationContext,"Double Click",Toast.LENGTH_SHORT).show()
    // Abstract class defining methods to check Double Click where Time Delay 
    // between the two clicks is set to 300 ms
    abstract class DoubleClickListener : View.OnClickListener {
        var lastClickTime: Long = 0
        override fun onClick(v: View?) {
            val clickTime = System.currentTimeMillis()
            if (clickTime - lastClickTime < DOUBLE_CLICK_TIME_DELTA) {
            lastClickTime = clickTime
        abstract fun onDoubleClick(v: View?)
        companion object {
            private const val DOUBLE_CLICK_TIME_DELTA: Long = 300 //milliseconds


Output on the Emulator:

Last Updated : 18 Feb, 2021
Like Article
Save Article
Similar Reads