Extension functions are a really cool feature that Kotlin provides, and that you’ll find yourself using a lot when writing Android Apps.

We have to admit that the Android Framework sometimes makes things a bit difficult, and in Java, the only solution we have left is to create wrappers that do things as we want, or utility classes with static methods that are not the best in terms of readability.

How would you like to be able to add extra functions to the Framework classes? This is what the Kotlin extension functions allow us to do.

If you prefer, I recorded a video where I explain extension functions, but also lambdas with receivers: an extra concept that you will find exciting!

Extension functions in Kotlin

Extension functions are functions that, as the name implies, help us to extend the functionality of classes without having to touch their code. Now let’s see how these functions are defined, and some examples that I personally find very useful.

How can you define an extension function?

Just write a function as you would normally, and put the name of the class before separated by a point. These functions can be everywhere, usually in an Extensions file which doesn’t even need to contain a class.

Very simple example: we want to make a view have the visible(), which makes it visible. We would write something like this:

Want to learn Kotlin?

Check my free guide to create your first project in 15 minutes!

I’ve written this so you can see that we can use the functions and properties of that class as if we were inside the class itself, but you can skip it:

Some interesting examples

There are a couple of examples that I like to explain because they perfectly show the power of extension functions.

The first is useful when you are inflating a view inside an adapter. Normally you would use something like this:

The line that inflates the view and uses parent is too complex, and 99% of the time is usually the same on any adapters. Why not give ViewGroups the ability to inflate views?

Now you can use it in the code above:

A very similar example can be done with images. If you use the Picasso library, for example, you need to load the image using the typical builder:

How would you like to be able to tell ImageView to upload a URL by itself?

Extension properties

Just as you can do extension functions, so can you do with properties. The only thing you need to remember is that extension properties cannot save state, but will need to use existing functions to request or modify the state of the object:

This property retrieves the children of a ViewGroup.

Now you could iterate over them directly:

Note: it is a reserved word that is used to access the input value of the function, when there’s only one. As we have seen in other articles, you can name those input values, and write the left par of the lambda when there are more than one.


With extension functions and extension properties, you can extend any class (even if you don’t have access to the code) and then use those functions and properties as if they were part of the class. The only thing you will see is an extra import in the file where it’s used.

If you like what you’ve seen, I encourage you to sign up for my free training, where I’ll tell you everything you need to learn about how to create your own Android Apps in Kotlin from scratch.



%d bloggers like this: