What Is Meaning of Boolean Value Returned from an Event-Handling Method in Android

What is meaning of boolean value returned from an event-handling method in Android

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.

Meaning of the return value of OnTouchEventListener

I found that "True if the listener has consumed the event, false
otherwise." But I am not getting what does that mean by "consumed the
event"?

Consuming the event means that its listener won't pass this event to further listeners, because this particular listener consumed it.

So, what are those further listeners, they can be one or more listeners; for instance when you tap a view, then the onTouch() callback method of the OnTouchEventListener of this particular view is triggered, and if you return true, then this listener consumes the event, so the next listener(s) callbacks won't be triggered; which is in our case the onClick() callback of the OnClickListener of this particular view that won't be called.

More Illustration:

Assume you've a button, and you registered below event listeners for it.

    Button myButton = findViewById(R.id.button1);
myButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Toast.makeText(Activity1.this, "Touched", Toast.LENGTH_SHORT).show();
return true; // Consumes the event
}
});

myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(Activity1.this, "Clicked", Toast.LENGTH_SHORT).show();
}
});

Now when you tap that button, only the Touched toast will be shown, and not the Clicked toast, because you return true from onTouch()

If you return false from onTouch(), then both Touched and Clicked will be shown.

The same applies for View.OnLongClickListener

Android - Why does onItemLongClick(...) return a boolean?

As you may know, the View hierarchy in Android is represented by a tree. When you return true from the onItemLongClick() - it means that the View that currently received the event is the true event receiver and the event should not be propagated to the other Views in the tree; when you return false - you let the event be passed to the other Views that may consume it. Hope this helps.

Difference between consuming and handling an event

Event handling: When an event happens and we have registered an event listener for the event, the event listener calls the Event Handlers, which is the method that actually handles the event.

OnPreferenceClickListener: what does the boolean return value do?

It means that when someone clicks the preference your listener has a chance to deal with the click.

If you handle the click you return true.

If you do not handle the click i.e. you don't do anything when this is clicked you can return false and the system will then pass the click on to the next listener that is listening for an on click for that preference.

I can't think of an example where you would listen for the click then not handle it sorry.

Event listener and event dispatching process

This is used when there are Views on top of other Views. Imagine there are a stack of Views as follows:

View A
View B

So the user would see View A and then View B would be under it. If View A has an onTouch listener, it will get invoked whenever a touch occurs on that View. If false is returned, then the touch event will be passed to View B and View B's onTouch listener (if it has one), can then go and handle the touch event.

Does that make sense?

The meaning of returning false from OnGestureListener.onDown()

The only time you should return false from onDown() is if you truly want to ignore an entire gesture.

That pretty much says it all.

The point is the onDown(...) method receives a MotionEvent as a parameter, You have the option to analyse the MotionEvent in the onDown(...) method and if it isn't something you want to handle then you return false.

A MotionEvent carries a lot of detailed information which includes position of the start of the gesture (for example) - if it's outside of an area you want to handle then return false otherwise return true.

If you return true from onDown(...) the other methods will then be called. Each of those methods again have the option to analyse and handle the various parameters passed to them. If you handle an event in any of those methods and don't want any further action then return true from those methods otherwise the other methods will be called (possibly in a super class depending on your code implementation).

Gestures are complex and involve down and up actions as well as movement in any direction. Allowing the option to reject a gesture (by returning false in onDown(...)) makes things more versatile.

EDIT: In some situations there may be a case where you have multiple views on a screen. The MotionEvent passed to onDown(...) will contain information about where the gesture starts. If you don't wan't some areas of your screen to react to gestures then you return false when you've checked the start position of the gesture.



Related Topics



Leave a reply



Submit