You’re probably tired of working with findViewById in your day-to-day life to recover Android views. Or maybe you gave up and started using the famous Butterknife library. Then you’ll love Kotlin Android Extensions.

Kotlin Android Extensions

Kotlin Android Extensions are another Kotlin plugin that is included in the regular one, and that will allow to recover views from ActivitiesFragmentsViews in an amazing seamless way.

Let’s see how easy it is.

1. Integrating Kotlin Android Extensions in our code

Though the plugin comes integrated into the regular one, if you want to use it, you’ll need to add an extra apply in the Android module:

And that’s all you need. You’re now ready to start working with it.

2. Recovering views in an Activity or Fragment

From this moment, recovering a view is as easy as using the view id you defined in the XML directly into your activity or fragment.

Imagine you have an XML like this one:

As you can see, the TextView has welcomeMessage id.

Justo go to your MainActivity and writ it:

To be able to use it, you need an special import (the one I write below), but the IDE is able to auto-import it. Couldn’t be easier!

The code that the plugin generates is able to store a view cache, so if you ask the view again, this won’t require another findViewById.

3. Recovering views from another view

Let’s say we have a view like this:

And that you’re inflating inside an adapter, for instance.

You can also access the subviews directly, just using this plugin:

Though the plugin will also help you fill the import, this one is a little different:

There are a couple of things you need to know about this:

  1. In compilation time, you’ll be able to reference any view from any other view. This means you could be referencing to a view that is not a direct children of that one. But this will fail in execution time, when it tries to recover a view that doesn’t exist.
  2. In this case, the views are not cached as it did for Activities and Fragments.

But if you use this carefully, it can be a really powerful tool.


You’ve seen how easy is to deal with Android views in Kotlin. With a simple plugin, we can forget about all that awful code related to view recovery after inflation. This plugin will create the required properties for us casted to the right type without any issues.