You can define widgets like button both by xml and programatically. Can has given a capabity to add the listener both ways. So there is no advantage of one over another.
If you want to do layout specific work from xml, the android has given you a capability to do that.
But someone may define layout progmatically and then will have to define click listener from code.
But there are people who use a mix of it.
I hope you understand what I mean.
If you do it programatically, you can just write onClick() and iside that write a switch case and based on view ids you can define the behaviour which i personally feel is easier to work with.