What causes a MotionEvent.ACTION_CANCEL in Android?
Asked Answered
A

5

49

I am working through debugging some touch handling stuff on Android, and am trying to figure out why the MotionEvent sent to my View's onTouchListener contains a cancel action. I have not been able to find any documentation on its cause, and was hoping someone could point me in the right direction for debugging this problem - error codes, source code, or some general knowledge.

Arawakan answered 14/8, 2012 at 21:16 Comment(2)
What do you mean by cancel action? What code are you using?Stokehole
@0gravity, I mean if event.getAction() == MotionEvent.ACTION_CANCELArawakan
S
69

Is this what you are looking for:

"ACTION_CANCEL occurs when the parent takes possession of the motion, for example when the user has dragged enough across a list view that it will start scrolling instead of letting you press the buttons inside of it. You can find out more about it at the viewgroup documentation: onInterceptTouchEvent."

Hope that is the answer you are looking for:

Resources: Motion Event, Stack Overflow.

Stokehole answered 14/8, 2012 at 21:29 Comment(1)
It also happens on rotation, which wasn't clear to me from the above.Tarrel
O
20

All you need is to call

requestDisallowInterceptTouchEvent(true);

on the parent view, like this -

        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            view.getParent().requestDisallowInterceptTouchEvent(true);
            switch(motionEvent.getActio){
            }

            return false; 

         }

Source: onInterceptTouchEvent, onTouchEvent only see ACTION_DOWN

Ophthalmologist answered 5/4, 2019 at 0:14 Comment(0)
V
2

ACTION_CANCEL is triggered by ancestor to notify all descendants that they lost onTouch control and it's will be responsible for handling the next onTouch event. Usually it is caused when a descendant returned true in onTouch or onTouchEvent method but after that, during the next of touch event of gesture, an ancestor returned true in onInterceptTouchEvent()

[Touch event flow]

Vav answered 26/7, 2019 at 14:41 Comment(0)
K
0

In my situation, TouchDelegate helped.

https://medium.com/android-news/android-change-touch-area-of-view-by-touchdelegate-fc19f2a34021

private fun changeTouchableAreaOfView(view: View, extraSpace: Int) {
val parent = view.parent as View
Observable.just(parent)
        .observeOn(AndroidSchedulers.mainThread())
        .subscribeOn(AndroidSchedulers.mainThread())
        .subscribe {
            val touchableArea = Rect()
            view.getHitRect(touchableArea)
            touchableArea.top -= extraSpace
            touchableArea.bottom += extraSpace
            touchableArea.left -= extraSpace
            touchableArea.right += extraSpace
            parent.touchDelegate = TouchDelegate(touchableArea, button)
        }

/* In case you don't want to use Rx java
parent.post {
    val touchableArea = Rect()
    button.getHitRect(touchableArea)
    touchableArea.top -= extraSpace
    touchableArea.bottom += extraSpace
    touchableArea.left -= extraSpace
    touchableArea.right += extraSpace
    parent.touchDelegate = TouchDelegate(touchableArea, button)
}
*/

}

Kalmia answered 26/10, 2021 at 9:32 Comment(0)
A
-1

When the drag moves out of view rect, you get ACTION_CANCEL

Augmenter answered 14/8, 2012 at 21:29 Comment(1)
This does not happen at all times, even when the finger is already outside the boundaries of the touched view ACTION_CANCEL won't be called but ACTION_UP.Defamation

© 2022 - 2024 — McMap. All rights reserved.