abdul rafay abdul rafay - 6 months ago 23
Android Question

Returning true in setOnLongClickListner

Can any one please tell me why it is necessary to return true in the end of setOnLongClickListner(); when I try to change the return type from Boolean to void then it gives me an error.

Answer
Can any one please tell me why it is necessary to return true in the end of setOnLongClickListner();

It is not necessary to return true, it is necessary to return a boolean, so it can be false.

From the docs:

Returns boolean true if the callback consumed the long click, false otherwise.

This means, if you return true the event stops propagating. Otherwise, the event keeps propagating and other related events will be called.

For example, if you return false, your OnLongClick event gets fired. After finishing, your onClickListener will be triggered. If you return true, event will stop in OnLongClickListener.

when I try to change the return type from Boolean to void then it gives me an error

You are implementing an interface. The method is already defined, but not implemented. You cant change it. For more info about this:

https://docs.oracle.com/javase/tutorial/java/concepts/interface.html