Ontouch event of OnTouchListener gets called twice in android

Android

Android Problem Overview


I am creating an appliction in which a line gets generated between two points given at runtime.
The problem that I see is that onTouch() is called twice for every click on my simulator. I know that two actions (ACTION_DOWN & ACTION_UP) are checked. But I want my app to call onTouch() just once. Please give me some ideas. This is the code that I used:

SurfaceView surfaceview = new SurfaceView(getContext());
SurfaceHolder h = surfaceview.getHolder();
int action = event.getActionMasked();
synchronized(h) {
    if (action == MotionEvent.ACTION_DOWN && action!=MotionEvent.ACTION_CANCEL)// && flag==true)
    {
        Log.d("TouchView","ACTION_DOWN ");
        Point pointer = new Point();
        pointer.x = (int) event.getX();
        pointer.y = (int) event.getY();
        touchPoint.add(pointer);
        view.invalidate();
        Log.d("MotionEvent.ACTION_DOWN", "point: " + pointer);
        action = MotionEvent.ACTION_CANCEL;
        flag = false;
    }
    else if(action == MotionEvent.ACTION_UP && action!=MotionEvent.ACTION_CANCEL)// && flag==true)
    {
        Log.d("TouchView","ACTION_UP");
        Point pointer = new Point();
        pointer.x = (int) event.getX();
        pointer.y = (int) event.getY();
        touchPoint.add(pointer);
        view.invalidate();
        Log.d("MotionEvent.ACTION_UP", "point: " + pointer);
        action = MotionEvent.ACTION_CANCEL;
        flag = false;
    }
    else return false;
}

Android Solutions


Solution 1 - Android

touchListener will be called for every MotionEvent.ACTION_DOWN, MotionEvent.ACTION_UP, and MotionEvent.ACTION_MOVE . so if you want to execute code only once , ie MotionEvent.ACTION_DOWN then inside

onTouch()
 if (event.getAction() == MotionEvent.ACTION_DOWN) {
//your code 
}

Solution 2 - Android

Or just use onClickListener:

        myButton.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				//do what you gotta do
			}
		});

Solution 3 - Android

Sometimes dealing with many views under the same parent cause the onTouch called many times (if they're above each other) the solution for me was

onTouch{ ...
        if(event.getAction() == MotionEvent.ACTION_DOWN && isTouchEnabled()){
            enableTouch(false);
            //add your code here 
            
            //then enableTouch at the end 
            this.postDelayed(new Runnable() {
                @Override
                public void run() {
                    enableTouch(true);

                }
            }, 500);
      }
      add static variable touch
    private static boolean enabled = true;
      
    private void enableTouch(boolean enabled){
        this.enabled = enabled;
    }

    private boolean isTouchEnabled(){
        return enabled;
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAbhishekBView Question on Stackoverflow
Solution 1 - AndroidShailendra Singh RajawatView Answer on Stackoverflow
Solution 2 - AndroidDpedrinhaView Answer on Stackoverflow
Solution 3 - AndroidFarido mastrView Answer on Stackoverflow