35

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

Shade
  • 9,936
  • 5
  • 60
  • 85
Lalith
  • 19,396
  • 17
  • 44
  • 54

6 Answers6

50

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 2
    Could you please let me know any references for this? – Lalith Dec 27 '10 at 23:41
  • 2
    @Lalith: The composite pattern is a classic design pattern: http://en.wikipedia.org/wiki/Composite_pattern – CommonsWare Dec 27 '10 at 23:46
  • Hmm.. the main reason i need multiple event handlers for single event is I plan to create a library which will be used by other android developers. It is supposed to be initialized somewhere and it goes all by itself attaching required event handlers to each View item. I did this in silverlight easily so was wondering if htat is possible. – Lalith Dec 27 '10 at 23:49
  • @Lalith: Yes, it is possible. – CommonsWare Dec 28 '10 at 00:01
  • Got it! but not so sure if I could follow this approach. Thanks any way ! – Lalith Dec 28 '10 at 00:41
  • 3
    For an example of how to do this: http://stackoverflow.com/questions/5465204/how-can-i-set-up-multiple-listeners-for-one-event – Dan Is Fiddling By Firelight Jan 09 '13 at 14:32
  • @Lalith, I'm also doing the same, so can you give me more insights about implementation. How did you implement this? – Suresh Apr 20 '20 at 05:34
  • John Kamau gives a code example for this below. But it's probably better practice to just have one onClickListener that calls multiple functions. – Nick Oct 22 '20 at 23:50
4

Should someone bump into a similar problem, try this out:

private void setClickListeners(View view, List<View.OnClickListener> clickListeners){
    view.setOnClickListener(v -> {
        for(View.OnClickListener listener: clickListeners){
            listener.onClick(v);
        }
    });
}
John Kamau
  • 37
  • 3
1

An easy way to achieve this would be simply do:

btn1.setOnClickListener(new View.OnClickListener(){
 @Override
 public void onClick(View v) {  
   listener1.onClick(v);
   listener2.onClick(v);
}})

Or call listener2.onClick(v) inside the definition of your listener1

Tom
  • 305
  • 5
  • 24
0

This is what I'll do in Kotlin:

arrayListOf(
        view1,
        view2,
        view3
    ).forEach { it.setOnClickListener { /*do stuff here*/ } }

very readable, and copy-paste error safe.

Stack Diego
  • 1,309
  • 17
  • 43
-1
public void onClick(View v) {
    if(v.getId() == R.id.button1) {
        // do this
    }else if(v.getId() == R.id.button2) {
        // do that
    }
}
Mike
  • 31
  • 1
-5

Nope, for example just do this :

Set Listener:

btn.setOnClickListener(this);

Implement Method:

public void Onclick(View arg0){

   // check your id and do what you want
}
Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
Tsunaze
  • 3,204
  • 7
  • 44
  • 81