Double-Tap on a Button in Android

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.
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:app="http://schemas.android.com/apk/res-auto"    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.Bundleimport android.view.Viewimport android.widget.TextViewimport android.widget.Toastimport 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        }    }}
Android-Misc
Android
Kotlin
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Add Views Dynamically and Store Data in Arraylist in Android?
Android SDK and it's Components
How to Communicate Between Fragments in Android?
Flutter - Custom Bottom Navigation Bar
Retrofit with Kotlin Coroutine in Android
How to Add Views Dynamically and Store Data in Arraylist in Android?
Android UI Layouts
Kotlin Array
How to Communicate Between Fragments in Android?
Retrofit with Kotlin Coroutine in Android