Journey with Kotlin 002.5 : Ways to create OnClickListener

Apparently, there are 4 ways in total

Jimmy Liu
4 min readOct 29, 2019

In Journey with Kotlin 002, we talked about how to create a button and how to set an OnClickListener for it. Today, I want to show you other ways to create an OnClickListener.

The button we want to apply the OnClickListener is called rollButton .

As mentioned in Journey with Kotlin 002, we can set the button’s OnClickListener by :

Method 1 — Anonymous Function

  • implementing the OnClickListener while setting it, the implementation is called an anonymous function and setOnClickListener is called a high order function .

Method 2 — Interface Type

Instead of using a member class, we can create a variable with OnClickListener type and simply use it as the parameter.

Or we can also adapts the View.OnClickListener Interface by the MainActivity Class.

Method 3 — Active Implementation of OnClickListener interface

In this method, we will make the current Activity adapts View.OnClickListener interface and set this as the OnClickListener for the button

Other than the methods above, you can also set the OnClick event in XML.

Method 4 — Android Default onClick Event (XML)

This is the simplest method to create the onClick Event. All you need to do is to set the onClick method in the layout xml file:

As you can see, the IDE will try to find the corresponding function, rollTheDice , in the View’s context. When the function is not found, error would occur. In order to silence this error, we need to create the function in its activity, in this case, the MainActivity. This is done simply clicking on the warning icon on the left and select “Create `rollTheDice(View)` in`MainActivity`”:

You can now implement the function and everything would work.

Last but not least, you can also create a member class that adapts View.OnClickListener Interface.

Method 5 — Member Class

We can also create a member class to act as the OnClickListener

Member Class : a class that is defined within another class

Since ButtonActions is a private class, we also need to make sure the constant of buttonActions is also private.

Summary

There are 5 ways to implement OnClickListener for a button.

2 of them required the implementation of View.OnClickListener Interface. It can be implemented by either Activity Class (Active Implementation) or Member Class.

2 of them uses High Order Function to take either a Anonymous Function or an Interface Type as a parameter.

The last one simply uses XML and add android:onClick = "__functionName" inside the View of interest. Then add a function with that name into the Activity of interest, and that’s that.

Overall, the implementation of OnClickListener is straight forward. However, sometimes it depends on the implementation’s reusability. If you are hoping to use the same implementation else where, then making a class might be a better choice.

Thank you for your time. If you find this article helpful, please give me a clap, otherwise, feel free to write any comment or question at below. Appreciated

--

--

Jimmy Liu

App Developer who enjoy learning from the ground up.