Search code examples
androidtouch-eventontouch

onTouch and onTouchEvent methods in Android


I wrote like below with 'onTouch' method.

But 'getPointerCount()' and 'getAction()' spits out the same values all the time.

This code can't recognize multi-touch.

And 'onTouch' method isn't called when MotionEvent.ACTION_UP occurs.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    relativelayout = (RelativeLayout) findViewById(R.id.relativeLayout);

    relativelayout.setOnTouchListener(new OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            Log.i("test", "multiTouchCount : " + event.getPointerCount());
            Log.i("test", "action : " + event.getAction());
            return false;
        }
    });
}

So, I tried the same thing with onTouchEvent method and this works.

public boolean onTouchEvent(MotionEvent event) {

    Log.i("test", "multiTouchCount : " + event.getPointerCount());
    Log.i("test", "action : " + event.getAction());

    return super.onTouchEvent(event);
}

What's the problem with 'onTouch' method?


Solution

  • In the onTouch() method of the Listener, you unconditionally return false, which causes the View to no longer receive touch events after the first ACTION_DOWN event, until ACTION_DOWN happens again.

    If you want to ensure that the View will continuously receive multi-touch events, return true unconditionally.