Mike G
Mike G

Reputation: 4959

How to capture all motion events using a listener/listeners

I would like to capture all motion events from the screen using a listener for example if i do a swipe on a screen from top to bottom there will be a touch down, touch move and a touch up. Since this touch will be over multiple views example linear view, buttons and text fields i tried to attach on touch listeners to all views but i would get bad data for example i would get touch up without touch downs and so on. Please advise a way that will achieve this.

Upvotes: 0

Views: 2379

Answers (2)

jobesu
jobesu

Reputation: 620

A good way to handle this is to attach the OnTouchListener to the parent ViewGroup (layout) of all your views.

For example a RelativeLayout that have multiple views (linear view, buttons and text fields) as you mention in your question. You can do like that:

RelativeLayout currentView = (RelativeLayout) findViewById( R.id.MyRelativeLayout );
currentView.addView(aSubView);
currentView.addView(aSubButton);
currentView.addView(aSubTextView);

currentView.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // Here you will receive all the motion event.
        return false;
    }
});

Upvotes: 5

ol_v_er
ol_v_er

Reputation: 27284

You can override the dispatchTouchEvent(MotionEvent ev) method in you activity.

It's called before the MotionEvent is forwarded to the different Activity Views.

You can then do what you want with it, forward them, consume them...

Upvotes: 0

Related Questions