DEV Community

Cover image for Android: Accessing view, now what? (butterknife, viewbinding, databinding comparison)
Emma Ham
Emma Ham

Posted on

Android: Accessing view, now what? (butterknife, viewbinding, databinding comparison)

Hey guys! It has been a while since the last post, hope you guys have been doing good and staying strong:)

Recently, I have come across multiple ways to access layouts and bind those views with code so that we can use that views as we want. Sounds like we are going into the topic too straight away?


Alt Text


Well let me put this way, I have a xml file which has text field. In one of the fragments that I have, I want to set the text value or even decide on the visibility of the text field. Sounds like I should get the instance of the view in my fragment class. Then how do we do it? how do we create a bridge/connect these two seperate files and use one property in the other class?

well again, like I said word "multiple" earlier, there are many. And needless to say if you are working on a exisiting project that has been there for a while, you might probably have come across the history of view binding in Android development(with a bit of exaggeration):)

In this article, we will cover an overview of some of the most common and well used ways to make above possible in Android Development and talk about the general concept and some of the points that we can take into our accounts going forward.
Especially, if you are trying to migrate or transit the way of it from old way to another way, then hopefully this article can give you a good insight.

Step1: What are there for us

There are five most common and well-known ways to do view binding.
Alt Text

I am sure that most of you are already aware of some of or all of these view binding libraries and possibly already been using it for a while. I am not going to go through each and every item and explain since it is just all about high level of the concept of view binding ways and just to give it a good understanding as myself was also confused since there were so many! (as you can see a bit of a frustration from the title "now what!")

A. FindViewByID

Alt Text

Here we go, we have the most traditional and oldest way to bind views here. Initially, the traditional steps to take to be able to bind views with code is something like this.

  1. Inflate the XML
  2. Find the required element from the XML
  3. Assign it to a local/member variable
  4. Get the value from data
  5. Assign the value or Assign the event listener

On the second step when finding the required element from the XML, using this old way would required to code "findViewById", creating unnecessary boilerplates.This approach is being considered as outdated way and better to avoid to use.

B. Butterknife

Alt Text
Butterknife was introduced in 2016 by Jake Wharton ever since its release, it had been considered one of the most best ways to replace "FindViewByID" since it was reducing a lot of boilerplates codes that original way was producing.
In butterknife, there are Annotate fields with @BindView and a view ID for Butter Knife to find and automatically cast the corresponding view in your layout like above.

However, the story doesn't end here
Guess what,
Butterknife tool is now deprecated and feature development and general bug fixes have stopped as the author himself is recommending to switch to "viewbinding" which will be covered in this article later.(FYI, this author has now joined the team to work on viewbinding tool now).
If you are interested to have a look at some documentations. check out this link.
https://github.com/JakeWharton/butterknife

C. Kotlin Synthetic

Alt Text

While butterknife was considered one of the best ways to do viewbinding. We also need to have a look at this one called Kotlin Syntehtic(also called Kotlin extensions). This one is, still in a lot of places, being considered the best way to do viewbinding for kotlin classes.

Why?
Kotlin uses synthetic properties, and they are called on demand with a caching function (hence a small activity / fragment load), and ButterKnife binds all the views at the same time ButterKnife.bind() (which consumes a bit more time). With Kotlin you don’t even need to use annotation to bind to views.
Just use the id of the imported views!!!

Yes, it also goes well with the RecyclerView + ViewHolder template, you just need to import kotlinx.android.synthetic.main.layout_main.view.* (If layout_main.xml is the name of the Activity / Fragment layout file).

You do not need to make any extra effort to import the layout with include .
Just use the id of the imported views.

Alt Text

Sounds good so far? You are about to see more fascinating stuff

D. ViewBinding VS Databinding

Here is why I wanted to explain viewbinding and databinding together.
Alt Text

As you can see, viewbinding is a sort of subset of databinding libraries which means viewbinding and databiding can do the same jobs in terms of binding layouts. And it would also mean with databinding, you might not need viewbinding cause it will do what viewbinding is supposed to do and also provide a bit of an extra functionalities such as 2way binding, and using variables in xml files.

then, this can lead to a question
"Then let's just use databinding as it sounds much more fancy!"

Hold on. As much as it sounds fancy, it is pretty heavy loaded library which might cause a longer compile time. So if you are not going to use databinding only functionalities then might be better consider viewbinding as it does have some advantages in terms of build time and apk size.

Also, apparently there are different steps need to take whenever using viewbinding functionalities with actual viewbinding library and databinding libarary.
Here are some examples.

** Adding tag in xml file
When we try to bind layouts using databinding, we need to go to that specific layout file and have to add tag manually to be able to get the generated binding class which I know a lot of developers hate to do.

** DataBindingUitil vs ActivityMainBinding(this one depends on the layout file name for example if the layout name is layout_login_fragment, it will be LayoutLoginFragmentBinding)

if we are using these in Activity this will be inside of onCreate, if it is Fragment it will be inside of onCreateView .
Alt Text

Here are some more links you can check
https://hackernoon.com/android-butterknife-vs-data-binding-fffceb77ed88 butterknife vs databinding

https://medium.com/@hardianbobby/databinding-vs-viewbinding-simple-comparison-792fa8d72e8 databinding vs viewbinding.

And some simple comparison between all the libraries mentioned above.

Alt Text

We have covered briefly about viewbinding ways on a high level in this article. Hopefully it gives you a better understanding of accessing views in Android and a bit of idea on which way that you will implement based on comparison here.

If it is possible, would love to dig in on viewbinding and databinding a bit more and write something up on how to implement it or even replace one with the other.

Alt Text

Thanks for reading!

Top comments (0)