Android: Kotlin Android Extensions – View Binding

It’s annoying to call the findViewById() lines every time getting a layout or a View widget, sometimes too many use of these lines may mess with other codes. There are not few view binding libraries helped developers avoid this, but most of them requires some extra code in XML layout files. And then here comes the Kotlin Android Extensions View Binding.

What is Kotlin Android Extensions and View Binding?

View Binding is a feature that allows you to more easily write code that interacts with Views.

Kotlin Android Extensions is a plugin that does not require annotating fields (extra codes in layout for example) for each exposed View and it allow us to perform View Binding without having to add any other extra code.

What does Kotlin Android Extensions View Binding do?

Applying Kotlin Android Extensions can get rid off the findViewById() lines without any code changes, which avoids the maximum possible bugs caused by the function if the layout id is incorrect or null.

For example, this allows for the following code:

//Using R.layout.activity_main from the 'main' source set
import kotlinx.android.synthetic.main.activity_main.*

class MyActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        
        //Instead of findViewById(R.id.textView)
        textView.setText("Hello, world!")
    }
} 

textView is an extension property (the TextView widget’s @+id/) for Activity, and it has the same type as decalred in activity_main.xml (so it is a TextView widget)

How to use Kotlin Android Extensions?

  • Configure the dependency by adding below line to build.gradle file in your module in order to enable the Android Extensions Gradle plugin:
    apply plugin: 'kotlin-android-extensions'
  • Import synthetic properties — all widget properties for a specific layout in one go:
    import kotlinx.android.synthetic.main.[layout name].*

    The [layout name] indicates the file name of a layout.
    For example, if the layout file name is activity_main.xml, the import line can be

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

    Another example, if we want to call the synthetic properties on View. the import line can be:

    import kotlinx.android.synthetic.main.activity_main.view.*
  • Invoke the corresponding extensions, which are properties(view widget ids) named after the views in the XML file. Example code below:
    <TextView
        android:id="@+id/hello"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

    The TextView id or property named here is hello, so when you are trying to call this TextView, do:

    activity.hello.text="Hello World!"
    //The activity here is the class name(activity.kt for example) that uses a layout having the hello TextView

    And this is all for Kotlin Android Extension View Binding.

    See you next time!

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.