Open In App
Related Articles

Double-Tap on a Button in Android

Improve Article
Save Article
Like Article

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

Step 1: Create an Empty activity in Android Studio. To create one, follow this article- How to Create/Start a New Project in Android Studio?. Check if the primary language selected is Kotlin.

Step 2: In activity_main.xml, add a button which will detect the double tap.


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""
  <!--Declare a button which shall be used to detect double taps-->
        android:text="Double Tap"

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


package org.geeksforgeeks.doubletap_onbutton
import android.os.Bundle
import android.view.View
import android.widget.TextView
import android.widget.Toast
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        // Declare the button from the layout file as Text View
        // Since the method works only on Views
        val dBtn = findViewById<TextView>(
        // Implementing a DoubleClickListener on the Button
        dBtn.setOnClickListener(object : DoubleClickListener() {
            override fun onDoubleClick(v: View?) {
                Toast.makeText(applicationContext,"Double Click",Toast.LENGTH_SHORT).show()
    // This class has methods that check if two clicks were registered
    // within a span of DOUBLE_CLICK_TIME_DELTA i.e., in our case
    // equivalent 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:

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

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