Implementing the double tap on an ImageView

In this post I explain a simple example of an implementation of a double tap event on an ImageView that can be adapted with some changes for a TextView or an Activity.

    1. MainActivity.java, the main activity
      package eu.lucazanini.doubletap;
      
      import android.app.Activity;
      import android.os.Bundle;
      
      public class MainActivity extends Activity {
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
      	super.onCreate(savedInstanceState);
      	setContentView(R.layout.activity_main);
          }
      }
      
    2. res/layout/activity_main.xml, il layout per l’activity principale
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/LinearLayout1"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
          android:gravity="center">
      
          <eu.lucazanini.doubletap.DoubleTapView
              android:id="@+id/imageView1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:src="@drawable/ic_launcher"
              android:layout_gravity="center" />
      
      </LinearLayout>
      

      at the line 8 there is a View linked to the class DoubleTapView that extends an ImageView

  1. DoubleTapView.java
    package eu.lucazanini.doubletap;
    
    import android.content.Context;
    import android.support.v4.view.GestureDetectorCompat;
    import android.util.AttributeSet;
    import android.util.Log;
    import android.view.GestureDetector;
    import android.view.MotionEvent;
    import android.widget.ImageView;
    
    public class DoubleTapView extends ImageView {
    
        private class MyGestureListener extends
    	    GestureDetector.SimpleOnGestureListener {
    
    	@Override
    	public boolean onDoubleTap(MotionEvent event) {
    	    Log.d(TAG, "onDoubleTap");
    	    return true;
    	}
    
    	@Override
    	public boolean onDown(MotionEvent event) {
    	    return true;
    	}
        }
    
        private final static String TAG = DoubleTapView.class.getName();
        private GestureDetectorCompat mDetector;
    
        public DoubleTapView(Context context, AttributeSet attrs) {
    	super(context, attrs);
    
    	mDetector = new GestureDetectorCompat(context, new MyGestureListener());
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent e) {
    	return mDetector.onTouchEvent(e);
        }
    
    }
    

    at the line 17 the method onDoubleTap handles the double tap but you need the following four important points:

    • line 34: creation of an object of the class GestureDetectorCompat (mDetector)
    • line 13: the inner class that extends GestureDetector.SimpleOnGestureListener and an object of this class is passed as argument to the constructor of mDetector of the previous point
    • line 38: the method onTouchEvent of ImageView is overridden and calls onTouchEvent of GestureDetectorCompat (mDetector)
    • line 23: in the inner class that extends GestureDetector.SimpleOnGestureListener the method onDown is overridden and returns true

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.