OnClick and onClickListener | Sololearn: Learn to code for FREE!


OnClick and onClickListener

what would I lose if I used onClick method instead of onClickListener interface?

7/10/2018 10:54:53 AM

Ahmed Mohamed

4 Answers

New Answer


Hello, Ahmed Mohamed ! I'm not sure that the question is clear. onClickListener is the interface that defines the onClick() method. If you have a class that intends to listen for clicks, you need to implement the interface (if it does not already extend the class that does), and implement this method too. You must use both; they are not alternatives.


Using the event listener is generally better. It is much easier to have multiple onclick events if you use the listener, rather than the HTML attribute. You can add and remove events, without having to edit the onclick attribute (which is saved similarly to a string). However, some old browsers don't fully support the listener.


James I don't actually understand the multiple events part, I think I can have multiple events using the onClick method too


Alexander Sokolov i mean using the onClick attribute in xml then making a method in Java without writing the onclicklistener line of code