VOOZH about

URL: https://www.geeksforgeeks.org/kotlin/kotlin-android-extensions/

⇱ Kotlin Android Extensions - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Kotlin Android Extensions

Last Updated : 30 May, 2022

If you’ve been developing Android Apps for some time, you’re probably already tired of working with findViewById in your day-to-day life to recover views. Or maybe you gave up and started using the famous Butterknife library. If that’s your case, then you’ll love Kotlin Android Extensions. Kotlin has a built-in view injection for Android, allowing to skip manual binding or need for frameworks such as ButterKnife. Some of the advantages are a nicer syntax, better static typing, and thus being less error-prone. In your project-local (not top-level) build.gradle append extensions plugin declaration below your Kotlin plugin, on top-level indentation level.

buildscript {
...
}
id : "com.android.application"
...
id: "kotlin-android"
id "kotlin-android-extensions"
...

Using Views

Assuming we have an activity with an example layout called activity_main.xml:

We can use Kotlin extensions to call the button without any additional binding like so:

You can also import all ids appearing in layout with a * notation

// my_button can be used the same way as before

import kotlinx.android.synthetic.main.activity_main.*

Synthetic views can't be used outside of Activities/Fragments/Views with that layout inflated:

Product flavors

Android extensions also work with multiple Android Product Flavors. For example, if we have flavors in the build.gradle like so:

android {
 productFlavors {
 paid {
 ...
 }
 free {
 ...
 }
 }
}

And for example, only the free flavor has a buy button:

We can bind to the flavor specifically:

import kotlinx.android.synthetic.free.main_activity.buy_button

Painful listener for getting noticed, when the view is completely drawn now is so simple and awesome with Kotlin's extension

mView.afterMeasured {
// inside this block the view is completely drawn
// you can get view's height/width, it.height / it.width
}

Under the Hood

Comment
Article Tags:

Explore