Attaching multiple listeners to views in android?

Lalith picture Lalith · Dec 28, 2010 · Viewed 24.6k times · Source

Is it possible to attach multiple onClick listeners to buttons in android? Example:

btn1.setOnClickListener(listener1);
btn1.setOnCliclListener(listener2);

And when the button is clicked onClick event of both listener1 and listener2 are triggered?

In normal GUI applications I know that we can attach more than one event handler for an event. Is it possible for android too or is there any restrictions?

Regards, Lalith

Answer

CommonsWare picture CommonsWare · Dec 28, 2010

Android only supports one registered listener in general. However, you can easily create a listener that simply forwards the events to other listeners using the composite pattern.