Search code examples
androiddrag-and-droponclicklistenerontouchlistener

Android on set touch listener shows multi touch


I am developing an android app using drag and drop function. In my app having a button name as buttonOne. Some time i want to click the button and some time i want to drag and drop the button. So, i use setOnTouchListener. When i drag or double click (or touch) the button click count shows more than times. For example when i drag the button i want to shows the count one. But it shows more than one. Some time it happens on click my code shown below.

imageButtonOne.setOnTouchListener(new OnTouchListener() {@Override public boolean onTouch(View v, MotionEvent event) {
//clicked =1; 
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadow = new View.DragShadowBuilder(imageButtonOne);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    v.startDragAndDrop(data, shadow, v, 0);
} else {
    v.startDrag(data, shadow, v, 0);
}
Log.d("LOGTAG", "Touched");
if(SystemClock.elapsedRealtime() - c < 1000) { // SystemClock is shows system time.

    setPosition(position);
}
c=SystemClock.elapsedRealtime();
switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
    startX = event.getX();
    startY = event.getY();
    break;
    case MotionEvent.ACTION_UP:
    float endX = event.getX();
    float endY = event.getY();
    if (isAClick(startX, endX, startY, endY)) {
        Log.d("LockView", "clicked");
    } else {}
    break;
}
v.getParent().requestDisallowInterceptTouchEvent(true); //specific to my project
return false;
}
});

Here SystemClock.elapsedRealtime() is shows the system is used for avoiding multiple touch response.

But not getting proper result how to solve it? Please help me?


Solution

  • I got the answer. It is in the problem of SystemClock.elapsedRealtime() - c. I have change the c < 1000 to c > 10.

    Now the problem solved.