listener vs handler in android

scooby picture scooby · Jun 19, 2012 · Viewed 8.6k times · Source

What is the difference between a listener and handler?

I have searched a lot, but I couldn't find a suitable explanation. Where do I use a listener and where do I use a handler in Android?

I have gone through the following link as well:

Are event handler, event listener, and event registration all referring to the same thing?

Where can I get a comparative discussion of these two items? Also, can anyone tell me what are the different kinds of listener and handler available?

Answer

Blah0x7B9 picture Blah0x7B9 · Sep 28, 2012

The basic difference is that event handlers let the originating object itself do something in response to the event, whereas event listeners let other objects do something in response to events originating in the object.

For example: your activity has a button. If you want your activity to handle when someone touches the button, you use an event listener (by doing btn.setOnTouchListener(...)). BUT, if you want to create a specialized button (e.g. one that looks like a dog and barks when touched), you can create a subclass of Button and implement its event handler, onTouchEvent(...). In this latter case, the button itself will handle its touch event.