Is it possible to attach multiple onClick listeners to buttons in android? Example:
btn1.setOnClickListener(listener1);
btn1.setOnCliclListener(listener2);
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.
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
}
public void onClick(View v) {
if(v.getId() == R.id.button1) {
// do this
}else if(v.getId() == R.id.button2) {
// do that
}
}