Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Double-Tap on a Button in Android

  • Last Updated : 23 Feb, 2021

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.

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!

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

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">
  
  <!--Declare a button which shall be used to detect double taps-->
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Double Tap"
        />
  
</RelativeLayout>

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.

Kotlin




package org.geeksforgeeks.doubletap_onbutton
  
import android.os.Bundle
import android.view.View
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)
  
        // Declare the button from the layout file as Text View
        // Since the method works only on Views
        val dBtn = findViewById<TextView>(R.id.btn)
  
          
        // 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) {
                onDoubleClick(v)
            }
            lastClickTime = clickTime
        }
  
        abstract fun onDoubleClick(v: View?)
  
        companion object {
            private const val DOUBLE_CLICK_TIME_DELTA: Long = 300 //milliseconds
        }
    }
}

 

Output on the Emulator:




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!