android – onTouchEvent executes twice-ThrowExceptions

Exception or error:

Can anyone explain to me why is the onTouchEvent executed twice and how can I set it to run only once? I couldn’t find an explanation. Thanks.

public void onCreate(Bundle savedInstanceState) {
    _iv = new ImageView(this);

    _map = BitmapFactory.decodeResource(getResources(), R.drawable.image);



public boolean onTouchEvent(MotionEvent event) {
    double X = event.getX();
    double Y = event.getY();

    Toast.makeText(this, "X: " + X + " Y: " + Y, Toast.LENGTH_SHORT).show();

    return super.onTouchEvent(event);
How to solve:

It executes for every event. In this case it would be for the ACTION_DOWN and ACTION_UP event. It will also execute for the ACTION_MOVE event many, many times.

To have it only execute in one event, do something like this:

  case MotionEvent.ACTION_DOWN:
    ** CODE ** 
  case MotionEvent.ACTION_MOVE:
    ** CODE ** 
  case MotionEvent.ACTION_UP:
    ** CODE **


In the switch statement, return false for all the events you do not need.

Leave a Reply

Your email address will not be published. Required fields are marked *