How to Simplify your Android View Binding Delegation

What is Android View Binding and How to simplify its delegation?

Yoga C. Pranata
Nov 6, 2020 · 4 min read
Image for post
Image for post

View Binding is part of Android Jetpack. View Binding was introduced at the Android Talk at Google IO/19. Let’s learn more about this.

What is View Binding?

As said on their developer’s page,

In most cases, view binding replaces findViewById.

Why do we need it?

When we use findViewById , we need to declare the view variable for x times we need to use it. It makes a lot of boilerplate code inside your view Activity/Fragment. That is why View Binding came to provide a good way to access all views with only initializing one variable.

This is how we use the findViewById:

And this is how we use the View Binding:

See the difference?

Yep, you only declare the binding variable from the generated view binding class. And you can access all of the view ids from the binding variable. Other advantages of using View Binding are:

Type Safety: View Binding provide a generated method of the same type as defined in the XML layout. So no more typecasting.

Null Safety: findViewById is not null safe either. If you are using Kotlin, it will save your pain of checking all of the null variables. Instead, for Java users, it will make them grab their keyboards and slam it to their monitors :D

How to use the View Binding?

  1. Enable the viewBinding feature on your app/build.gradle :
android {
....
buildFeatures {
viewBinding true
}
}

2. After that, just create an XML layout. For example, create an activity_sample.xml.

3. Then, it will autogenerate the binding class and it will be accessed as ActivitySampleBinding class. Just for the record, If you don’t want the XML layout to be autogenerated, you can add tools:viewBindingIgnore="true" to your root layout:

<LinearLayout
...
tools:viewBindingIgnore="true" >
...
</LinearLayout>

4. After that, inflate your ActivitySampleBinding class with your layout inflater.

5. In your Fragment, set the binding inflater inside your onCreateView and return the binding.root. You can see the full example here.

6. Accessing your view, you can just do this:

binding.tvTitle.text = getString(R.string.sample_title)binding.btnSample.setOnClickListener {
showMessage("Sample Button Clicked")
}

PS: If your view id uses the under_score, it will be generated as a camelCase variable.

7. And that’s it. You are done setting up your view using View Binding.

Now for the next question is, How do you make the binding delegation with just one-liner?

Kotlin is an extraordinary language, to make the binding delegation with just one-liner, we need to use the one of Kotlin property. It is Kotlin Property Delegates. See the explanation here.

I create this library to help me achieve one-liner delegation, it is built using Kotlin Property Delegates and combined with the Kotlin Extension Functions. So no more boilerplate for View Binding delegation in your view. See the library here.

Adding dependencies:

Add this to your build.gradle:

allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}

enable viewbinding in your app/build.gradle:

android {
....
buildFeatures {
viewBinding true
}
}

After that, add the dependencies in your app/build.gradle:

dependencies {
....
implementation 'com.github.yogacp:android-viewbinding:x.x.x'
}

How to use the library

  1. Assume that we still have the XML layout above, activity_sample.xml.
  2. In your Activity, just add the by viewBinding() at the end of your binding variable

3. In your Fragment, for example, you create an XML layout fragment_sample.xml. And here how you delegate your binding variable:

And that’s it. You can access your view binding with the only call by viewBinding() delegation.

Conclusion

View Binding will give you null-safety and type-safety to your view. And no more typecasting every time you need it in your Activity/Fragment.

Also, the Android ViewBinding library will give you the same behavior as in the View Binding Documentation, but with much less code to cart around in your Activity/Fragment. This library is still under maintenance. Currently, the version is 1.0.1 and still have a TODO list inside it. I Will did it later in the near future.

Last but not least, Comments/Suggestions are welcome as always. Keep learning and keep sharing.

Stay Healthy and Happy Coding :)

References:

Easyread

Easy read, easy understanding.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store