Detect Double Tap Click On Screen In Android Programmatically

How use GestureDetector OnGestureListener GestureDetector OnDoubleTapListener in android app.

Double tap on activity screen is mostly used to send nudge in social networking chat android applications and also to perform some different type of functionality. In this tutorial we are going to detect the double tap on screen with the use of GestureDetector class . The method which we are using is onDoubleTap(MotionEvent event) . This is a pre inbuilt function of this class. So here is the complete step by step tutorial for Detect Double Tap Click On Screen In Android Programmatically.

android-project-download-code-button

How to Detect Double Tap Click On Screen In Android Programmatically.

Code for MainActivity.java file.

package com.android_examples.doubletap_android_examplescom;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener,
        GestureDetector.OnDoubleTapListener {

    GestureDetector gestureDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        gestureDetector = new GestureDetector(MainActivity.this, MainActivity.this);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event){
        this.gestureDetector.onTouchEvent(event);
        // Be sure to call the superclass implementation
        return super.onTouchEvent(event);
    }

    @Override
    public boolean onDown(MotionEvent event) {

        return true;
    }

    @Override
    public boolean onFling(MotionEvent event1, MotionEvent event2,
                           float velocityX, float velocityY) {

        return false;
    }

    @Override
    public void onLongPress(MotionEvent event) {

    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
                            float distanceY) {

        return false;
    }

    @Override
    public void onShowPress(MotionEvent event) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent event) {

        return false;
    }

    @Override
    public boolean onDoubleTap(MotionEvent event) {

        Toast.makeText(MainActivity.this, "Double Tap on Screen is Working.", Toast.LENGTH_LONG).show();

        return true;
    }

    @Override
    public boolean onDoubleTapEvent(MotionEvent event) {

        return true;
    }

    @Override
    public boolean onSingleTapConfirmed(MotionEvent event) {

        return false;
    }

}

Code for activity_main.xml layout file.

<?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:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.android_examples.doubletap_android_examplescom.MainActivity"
    android:background="#FFF3E0"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Detect Double Tap In Android Programmatically"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:textSize="25dp"
        android:textColor="#000000"/>
</RelativeLayout>

Screenshot:

Detect Double Tap Click On Screen In Android Programmatically

Click here to download Detect Double Tap Click On Screen In Android Programmatically project with source code.