OnTap listener implementation
Asked Answered
C

4

5

I have a doubt that is on tap listener implemented on a particular button, or image view or view? because the sites which I surfed only shows for the whole layout and I want my action to be performed on the tapping of a view. please help. Thank you.

Corollary answered 16/7, 2011 at 8:13 Comment(0)
W
11

Any view can be setup with an onClickListener() which is part of the view class. The easiest way to do it is when you setup the references to your view in the onCreate() method. Here is an example for a image view:

ImageView iv = (ImageView) findViewByID(R.id.example);
iv.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        // Do what you need to do on click
        ....
    }
});

UPDATE: DOUBLE TAP

Here is a sample activity which implements basic double tap detection on an image view:

import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class DoubleTapActivity extends Activity {
    //Set the double tap delay in milliseconds
    protected static final long DOUBLE_CLICK_MAX_DELAY = 1000L;
    private ImageView iView;
    private long thisTime = 0;
    private long prevTime = 0;
    private boolean firstTap = true;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        iView = (ImageView)findViewById(R.id.iView);
        iView.setOnTouchListener( new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                if(firstTap){
                    thisTime = SystemClock.uptimeMillis();
                    firstTap = false;
                }
                else
                {
                    prevTime = thisTime;
                    thisTime = SystemClock.uptimeMillis();

                    //Check that thisTime is greater than prevTime
                    //just incase system clock reset to zero
                    if(thisTime > prevTime){

                        //Check if times are within our max delay
                        if((thisTime - prevTime) <= DOUBLE_CLICK_MAX_DELAY){

                            //We have detected a double tap!
                            Toast.makeText(DoubleTapActivity.this, "DOUBLE TAP DETECTED!!!", Toast.LENGTH_LONG).show();
                            //PUT YOUR LOGIC HERE!!!!

                        }
                        else 
                        {
                            //Otherwise Reset firstTap
                            firstTap = true;
                        }
                    }
                    else 
                    {
                        firstTap = true;
                    }
                }
                return false;
            }
        });
    }
}
Whitley answered 16/7, 2011 at 8:40 Comment(2)
yes i know this code Kenny but what i m asking can DOUBLE TAP LISTENER be implemented on a particular view or for a whole screen. i am making a project in which i have to take action on the tap of only a view not the whole screen.Corollary
You never mentioned double tap anywhere in your question! But I've expanded my answer to show you one possible way of doing it.Whitley
S
2

Mostly to sense tap event you have to implement GestureDetector.OnGestureListener in your Activity. You can perform action by using onSingleTapUp(MotionEvent e) method of GestureDetector.OnGestureListener.

Generally for Button control, we use like

myButton.setOnClickListener(new OnClickListener() {});

or other clickListners and tap events are used for ImageView or any other customviews.

Satterlee answered 16/7, 2011 at 8:26 Comment(0)
T
1

Add android:onClick="tapEvent" in your layout which you want to take the click. By changing the MAX_TAP_COUNT value, you can use any number of taps.

private long thisTime = 0;
private long prevTime = 0;
private int tapCount = 0;
private static  final int MAX_TAP_COUNT = 5;
protected static final long DOUBLE_CLICK_MAX_DELAY = 500;


public void tapEvent(View v){

    if (SystemClock.uptimeMillis() > thisTime) {
        if ((SystemClock.uptimeMillis() - thisTime) > DOUBLE_CLICK_MAX_DELAY * MAX_TAP_COUNT) {
            Log.d(TAG, "touch event " + "resetting tapCount = 0");
            tapCount = 0;
        }
        if (tapCount()) {
           //DO YOUR LOGIC HERE
        }
    }
}

private Boolean tapCount(){

    if (tapCount == 0) {
        thisTime = SystemClock.uptimeMillis();
        tapCount++;
    } else if (tapCount < (MAX_TAP_COUNT-1)) {
        tapCount++;
    } else {
        prevTime = thisTime;
        thisTime = SystemClock.uptimeMillis();

        //just incase system clock reset to zero
        if (thisTime > prevTime) {

            //Check if times are within our max delay
            if ((thisTime - prevTime) <= DOUBLE_CLICK_MAX_DELAY * MAX_TAP_COUNT) {
                //We have detected a multiple continuous tap!
                //Once receive multiple tap, reset tap count to zero for consider next tap as new start
                tapCount = 0;
                return true;
            } else {
                //Otherwise Reset tapCount
                tapCount = 0;
            }
        } else {
            tapCount = 0;
        }
    }
    return false;

}
Tranquil answered 20/3, 2017 at 5:27 Comment(0)
P
0
@Override
public boolean onTouchEvent(MotionEvent event) {

    // on action down do your work...
    return super.onTouchEvent(event);
}
Pathogen answered 29/9, 2014 at 14:2 Comment(1)
You should add an explanation about your answer because it will help the author easy to understand.Capuano

© 2022 - 2024 — McMap. All rights reserved.