4 Ways To Implement OnClickListener On Android

Cody Engel
6 min readJun 28, 2017

Ever since I created my first Android app in 2011 (it was a name generator) I have relied heavily on implementing OnClickListener to make my apps work. More recently I have been exploring different ways to implement interfaces within classes and wanted to extend what I have learned in the form of different ways to implement this very common interface.

If you aren’t an Android developer then all you need to know is you can receive callbacks when a button or other view is tapped through OnClickListener which has a method named onClick. With that in mind you can replace OnClickListener with any other interface or protocol and this should still be relevant to you.

Here are four ways to implement OnClickListener in Android without the use of a third party library.

Thanks for the eye catching image, Jeremy Bishop.

Option One

The first option we’re going to look at involves defining our OnClickListener within the method call site which can be found starting at line 10. This is the way I learned to handle click events on views back in 2011. For several years I was fairly content with this method, but it is now something I almost never do.

--

--