I have tried the following code to differentiate single click and double click. Single click is ok. When I double click the imageview, code inside both the single click and doub
just a little change and my code works fine. I placed the mHasDoubleClicked boolean inside handler, that makes the trouble. The below code works.
img_feat_orgn_item.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
findDoubleClick();
if (mHasDoubleClicked) {
Log.v("double click","double click");
startActivity(new Intent(
Wv_HomePage.this,
NewDonation.class));
}
}
});
private boolean findDoubleClick() {
// Get current time in nano seconds.
long pressTime = System.currentTimeMillis();
// If double click...
if (pressTime - lastPressTime <= DOUBLE_PRESS_INTERVAL) {
mHasDoubleClicked = true;
// double click event....
} else { // If not double click....
mHasDoubleClicked = false;
Handler myHandler = new Handler() {
public void handleMessage(Message m) {
if (!mHasDoubleClicked) {
// single click event
feature_class_val = listData_Feature_Organization
.get(j);
Intent intent = new Intent(
Wv_HomePage.this,
OrganizationDetails.class);
Bundle b = new Bundle();
b.putString("orgn_name",
feature_class_val.name);
intent.putExtras(b);
startActivity(intent);
}
}
};
Message m = new Message();
myHandler.sendMessageDelayed(m, DOUBLE_PRESS_INTERVAL);
}
lastPressTime = pressTime;
return mHasDoubleClicked;
}
btn.setOnClickListener(new View.OnClickListener() {
volatile int i = 0;
@Override
public void onClick(View v) {
i++;
Handler handler = new Handler();
Runnable r = new Runnable() {
@Override
public void run() {
if (i == 1) {
//single click logic
i = 0; //set to zero
}
}
};
if (i == 1) {
handler.postDelayed(r, 300);
} else if (i == 2) {
handler.removeCallbacks(r);
i = 0;
//Double click logic
}
}
}
);
Here's my code, which seem to work.
import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ImageView;
public class MultiTapImageView extends ImageView{
private GestureDetector gestureDetector;
private MultiTapImageViewListener mListener;
public interface MultiTapImageViewListener {
void onDoubleTap();
void onSingleTap();
}
public MultiTapImageView(Context context, AttributeSet attrs) {
super(context, attrs);
gestureDetector = new GestureDetector(context, new GestureListener());
}
public void setDoubleTapListener(MultiTapImageViewListener listener){
mListener = listener;
}
@Override
public boolean onTouchEvent(MotionEvent e) {
return gestureDetector.onTouchEvent(e);
}
private class GestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onDown(MotionEvent e) {
return true;
}
// event when double tap occurs
@Override
public boolean onDoubleTap(MotionEvent e) {
float x = e.getX();
float y = e.getY();
if(mListener != null){
mListener.onDoubleTap();
}
return true;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
if(mListener != null){
mListener.onSingleTap();
}
return true;
}
}
}
Try this.
btn.setOnClickListener(new View.OnClickListener() {
volatile int i = 0;
@Override
public void onClick(View v) {
i++;
Handler handler = new Handler();
Runnable r = new Runnable() {
@Override
public void run() {
if (i == 1) {
//single click logic
}
}
};
if (i == 1) {
handler.postDelayed(r, 150);
} else if (i == 2) {
handler.removeCallbacks(r);
i = 0;
//Double click logic
}
}
}
);
Or you can check DOUBLE-TAP example from following URL. that is used in listView. i hope it is useful for you.
https://nodeload.github.com/NikolaDespotoski/DoubleTapListView/zip/master
By convention, Android apps don't have double clicking.
Maybe you'd rather use onLongClick?
Although, if you really want double click check out GestureDetector.OnDoubleTapListener