Steps to Enable Double Tap on a Button on Android

Follow the Steps to Create an Application using the double-tap functionality of the Android Button.

Step 1: Create an Empty activity in Android Studio.

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.

Step 2: Make Changes in Layout

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

activity_main.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: Setting the Acitivty to Enabling Double Tap Button in Android

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 class.

MainActivity.java
package com.gfg.double_click_button_java;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        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
        TextView dBtn = findViewById(R.id.btn);

        // Implementing a DoubleClickListener on the Button
        dBtn.setOnClickListener(new DoubleClickListener() {
            @Override
            public void onDoubleClick(View v) {
                Toast.makeText(getApplicationContext(), "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
    public abstract static class DoubleClickListener implements View.OnClickListener {
        private long lastClickTime = 0;

        @Override
        public void onClick(View v) {
            long clickTime = System.currentTimeMillis();
            if (clickTime - lastClickTime < DOUBLE_CLICK_TIME_DELTA) {
                onDoubleClick(v);
            }
            lastClickTime = clickTime;
        }

        public abstract void onDoubleClick(View v);

        private static final long DOUBLE_CLICK_TIME_DELTA = 300; // milliseconds
    }
}
MainActivity.kt
package com.gfg.double_click_button

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:TextView = findViewById(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 {
            //milliseconds
            private const val DOUBLE_CLICK_TIME_DELTA: Long = 300 
        }
    }
}

Output of the Application:


Note : To access the full android application using Pie chat check this repository: Double Click the Button Android



Double-Tap on a Button in Android

Detecting a Double Tap on Button in Android 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 the corresponding to it response is added in the form of toast.


Similar Reads

Steps to Enable Double Tap on a Button on Android

Follow the Steps to Create an Application using the double-tap functionality of the Android Button....

Contact Us