Implementing the OnTap Listener

I have a doubt that on the listening listener implemented on a certain button, either in the form of an image or in a view? because the sites that I was browsing show only for the whole layout, and I want my actions to be performed while listening to the view. please help. Thanks.

+3
source share
4 answers

Any view can be customized using onClickListener() , which is part of the view class. The easiest way to do this is to set up links to your view in the onCreate() method. Here is an example image:

 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 an example of an activity that implements basic double-touch detection in 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; } }); } } 
+9
source

Basically, in order to sense a click event, you should implement the GestureDetector.OnGestureListener in your activity. You can perform the action using the onSingleTapUp(MotionEvent e) GestureDetector.OnGestureListener .

Usually for Button control we use as

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

or other clickListners buttons and click events are used for ImageView or any other custom views.

+2
source

Add android:onClick="tapEvent" to your layout that you want to click. By changing the value of MAX_TAP_COUNT , 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; } 
+1
source

@Override public boolean onTouchEvent (MotionEvent event) {

  // on action down do your work... return super.onTouchEvent(event); } 
0
source

Source: https://habr.com/ru/post/909975/


All Articles