Attaching multiple listeners to views in android?

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);
        }
    });
}

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.


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