views:

79

answers:

3

In android, most event listener methods return a boolean value. What is that true/false value mean ? what will it result in to the subsequence events ?

class MyTouchListener implements OnTouchListener {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        logView.showEvent(event);
        return true;
    }
}

Regarding to the above example, if return true in onTouch method,I found every touch event(DOWN,UP,MOVE,etc) has been captured according to my logView. On the contrary,if return false, onely the DOWN event been captured. So it's seemd that return false will prevent the event to propagate. Am I correct ?

Furthermore, in a OnGestureListener, many methods have to return a boolean value too. Do they have the same meaning ?

A: 

The boolean value determines whether the event is consumed or not.

Yes you're correct. If you return false, the next listener handles the event. If it returns true, the event is consumed by your listener and not sent to the next method.

Falmarri
This is false. `true` means you consumed the event and want the rest of the events in the gesture - other listeners/views will not receive the events. `false` means let someone else handle the event. It's actually a bit more specific than that though; see my answer.
adamp
How is that not exactly what I said?
Falmarri
What you said is reversed. :)
adamp
-1 for what's clearly a typo?
Falmarri
A: 

From the documentation : http://developer.android.com/reference/android/view/View.OnTouchListener.html#onTouch(android.view.View, android.view.MotionEvent)

"True if the listener has consumed the event, false otherwise."

If you return true, the event is processed. If false, it will go to the next layer down.

Matthieu
+2  A: 

If you return true from an ACTION_DOWN event you are interested in the rest of the events in that gesture. A "gesture" in this case means all events until the final ACTION_UP or ACTION_CANCEL. Returning false from an ACTION_DOWN means you do not want the event and other views will have the opportunity to handle it. If you have overlapping views this can be a sibling view. If not it will bubble up to the parent.

adamp