In this article we will look at Android Extensions given by Kotlin Team and say our good-byes 👋 to findViewById

Android Extensions

We all Android Developers have been bored writing findViewById to bind the views. Kotlin Team realized it and they gave us Android Extensions.

Kotlin Android Extensions is a plugin by Kotlin Team. It eliminates the need of findViewById in your code.

Let’s look at current approach

Imagine we have a layout as shown below

Main Activity Layout

It’s Layout Binding (Kotlin)

Traditional way of binding layout in Kotlin

Get ready to say your goodbyes to findViewById

With Android Extensions

Kotlin Android Extensions

You might notice that we got toolbar view without even using findViewById. That’s because Kotlin (specifically Android Extensions) do the heavy lifting for you.

You can directly get the view by the id specified in the layout.

For example: We define id for Toolbar in the layout as android:id="+@id/toolbar" then we can play with Toolbar using the same id in Kotlin code.

PS: Did you notice the import statement added at the top?
It is where Kotlin is binding the view using findViewById internally


For the layout filename activity_main.xml, we'd need to import


Android Extensions are AWESOME!

Just tell me how to setup Android Extensions

Go to your gradle module file and add

apply plugin: 'kotlin-android-extensions'

That’s it! The setup is done!

That’s it!

Under the hood

Internally the compiler creates a small hidden cache function which callsfindViewById for every view on that Activity or Fragment giving us the synthetic property to use the view.

The synthetic properties are lazy initialized that means when you use the view in the code for which it will bind the view and give us the property. Next time you use the same view it will fetch it from cache instead of calling findViewById again.

Points to note

  • This Android Extension doesn’t work on MenuItem in case you were thinking to use for Menus.


I’m pretty sure Android Extensions have made your day :). In case your worried if this affects the performance of the app then let me tell you it doesn’t affect the performance of your app.

What’s next?

We will continue with this series and with each article will dive deep into amazing features of Kotlin.

Sneak Peak: The next article will be regarding lazy initialization which is another super cool stuff.

Google Developer Expert @ Android | Android Engineer @ Clue | Instructor @Caster.IO

The (retired) Pub(lication) for Android & Tech, focused on Development