Android: Accessing view, now what? (butterknife, viewbinding, databinding comparison)
Tags: android, kotlin, dev, beginners
Published: 2020-06-29 06:00:16

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? 
<br>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/asu3gx908sth9b6tlvjv.jpeg)
<br>
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. 

<h2>Step1: What are there for us </h2>
There are five most common and well-known ways to do view binding. 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/gehyeutfcr2zyp2cises.png)

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!")

<h3> A. FindViewByID</h3>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/cbbelntscjrsuzvry7d2.png)

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.
<ol>
<li>Inflate the XML</li>
<li>Find the required element from the XML</li>
<li>Assign it to a local/member variable</li>
<li>Get the value from data</li>
<li>Assign the value or Assign the event listener</li>
</ol>
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. 

<h3> B. Butterknife</h3>

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/efpnzkyzrdnep9iq6g5d.png)
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. 

<b>However, the story doesn't end here</b>
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

<h3> C. Kotlin Synthetic</h3>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ovmysnl2q4pzlc5gzllr.png)

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. 

<b>Why?</b>
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. 
<b>Just use the id of the imported views!!!</b>


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 . 
<b>Just use the id of the imported views.</b>

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/iatgodwxpc7xdklkpnw7.jpeg)
<center>Sounds good so far? 
You are about to see more fascinating stuff</center>

<h3> D. ViewBinding VS Databinding</h3>
Here is why I wanted to explain viewbinding and databinding together.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/7z4wq3spozijbljfsou6.png)

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 <b>extra functionalities such as 2way binding, and using variables in xml files.</b>

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 <layout> 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 <layout></layout> 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](https://dev-to-uploads.s3.amazonaws.com/i/z1q1lep86xux7mdjp47k.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/zpqkvqb4uooaf5xn9ctm.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/fy4a3p1766ozcuum79vg.png)
<center><h3>Thanks for reading!</h3></center>