How lambdas work in Kotlin. setOnClickListener transformation (KAD 18)

Although I spoke a little about it in another article, I’d like to explain in depth how lambdas work in Kotlin, and how they transform the interfaces with a single method in lambdas when we  are working with Java libraries.

In particular I’m going to show you some examples on how to simplify the use of the Android Framework, and we’ll see the setOnClickListener of the Android views in detail.

Would you like to start today to take the next step? I recommend that you sign up to my free training here.

SetOnClickListener transformation

One of the features I like most about Kotlin is that it simplifies the work with the Android framework thanks to some conventions.

For example, the setOnClickListener function, which in Java is defined as:

public void setOnClickListener(OnClickListener l) {

When we use it in Kotlin, it corresponds to the following:

fun setOnClickListener(l: (View) -> Unit)

This saves us the need to have to create an anonymous implementation of the interface, greatly simplifying the initialization of UI components.

Using setOnclickListener. The original way

Following the above, we’ve already saved enough code. This is what we would have if we had to create an anonymous class of OnClickListener:

view.setOnClickListener(object : View.OnClickListener {
    override fun onClick(v: View?) {

But you’ll see that the editor shows you a warning directly, and recommends using the lambda way.

This is the transformation you can do:

view.setOnClickListener({ v -> toast("Hello") })

Easier, right? But this can be simplified even more.

If the function’s last parameter is a function, it can go outside the parentheses

Therefore, we can extract the listener as follows:

view.setOnClickListener() { v -> toast("Hello") }

If we had more parameters, the rest of the parameters would go inside the parentheses, even if these were functions. Only the last parameter can be extracted.

If a function has only one parameter, and this is a function, the parentheses can be deleted

Instead of having empty parentheses, we can better delete them:

view.setOnClickListener { v -> toast("Hello") }

This comes great for building code blocks. In this way we can define DSLs that model our own language.

A very typical example is the Kotlin reference page, where they build a DSL to create HTML by code.

If you don’t use the parameter of a lambda, you can remove the left side of the function

This is true if you only have one parameter. The view (v) isn’t being used, so we can remove it:

view.setOnClickListener { toast("Hello") }

In addition, in functions that only receive a parameter, instead of defining the left side, we could use the reserved word it, saving some characters.

For example, if we used the view to pass it to another method:

view.setOnClickListener { v -> doSomething(v) }

We have the option to simply use it:

view.setOnClickListener { doSomething(it) }

Your friends, the Lambdas

You see that the code difference is quite important. And not only for the characters you save (about 70%), but it greatly improves readability.

Instead of having to skip all the code that does nothing to find the useful part, we just write what really matters.

Cheer up and come to my free training. I will tell you everything you need to learn about how to create your own Android Apps in Kotlin from scratch. Discover everything about the language of the future.

13 thoughts on “How lambdas work in Kotlin. setOnClickListener transformation (KAD 18)”

  1. > If you don’t use the parameter of a lambda, you can remove the left side of the function
    Wrong. You can remove parameter name only if lambda accepts a single parameter (and parameter will be called `it`, sou you can use it). If lambda accepts, for instance, two unused parameters, you will be forced to list them: `{ _, _ -> … }`.

  2. HI, I’m new to kotlin, this article is very helpful to understand lambda syntax 🙂

    I was little confused at this point,
    ” fun setOnClickListener(l: (View) -> Unit)”

    I interpreted l is of type View (bracket is misleading :P) and the function returns unit. I was looking for interface OnClickListener in the above method signature , then I realised I is a lambda function accepting View as parameter and with return type void (Unit). which should have been seen like this “l: ((View) -> Unit)” .

    If you wish you can explain this little detailed in the article. Thanks

    1. Didn’t you write the same, but with some extra parenthesis? Can you elaborate a little more, from what I see, it’s the same thing, but you don’t need those extra parenthesis you wrote.

  3. Jeffrey Peoples

    I’m curious about the relationship between the anonymous class instantiation — the original way — and the lamba. Is using the lamba in place of an anonymous class something that you can do in general for particular use cases in Kotlin, or is the lambda way something that kotlin for android provides specifically for setOnClickListener?

    1. It’s a Kotlin (and also Java 8) simplification, where an interface with just one method in Java can be used with lambda syntax. That applies to all interfaces with one method written in Java, not only to some specific tailor-made ones.

  4. Tamim Attafi

    Thank you for this simplified explanation, somehow I’m still confused about how can we send a callback from the listened class through a lambda listener to the listening class ?

    the java way, it would look like : listener.onClick(view);

    how would the Kotlin way look like ?

Comments are closed.