How to use ViewBinding in Android Studio 3.6!

Sana Ebadi
3 min readMar 5, 2020

View Binding instead of Databinding
ViewBinding in Android

It’s been a while since the release of Android Studio 3.6.1 on the Stable Channel, and it’s a very complete version with very good graphics and performance features, with a new feature called ViewBinding that comes with. Let’s talk about this release. So stay with me.

All of us Android developers were constantly scrambling to findViewById to define a view of XML in Java, and we were looking for a way to avoid it that many of us took to a third library called butterknife and partly It could keep us happy, but it still had some problems.

Of the ktx property of Kotlin I can also say that it made us feel comfortable writing findViewById, but it often bothered me with the Null error because it was run in runTime.

Well we got to databinding which solved most of the previous problems and added some new ones.

The use of databinding was not just for the convenience of findViewById, but it had other uses, but the overloading of the code caused problems such as having unknown and compile time errors, but we still used it.

Until ViewBinding was introduced and it came to solve the problem of databinding generation as well as some good features.

findViewById alternative

Null safety

Compile-time safety

Lighter & faster than Databinding

It’s simple and easy to use, just type this line of code in the build.gradle file in the android tag:

Then go to the layout.xml and assign the appropriate ID to the view:

Then look at the Activity.kt and paste the relevant codes as follows:

As you can see in the code above, ResultProfileBinding is a class created for our layer. Since the name of the layout.xml we are going to use is the Viewbinding property is result_profile.xml so the same name will be written above. For example, if my layer was called activity_about.xml we should have ActivityAboutBinding built for us.

Then by this line of code we attach the layer to it and inflate it as follows:

binding = ResultProfileBinding.inflate(layoutInflater)

Finally, introduce your layout using setContentView:

setContentView(view)

We’re done here! If you are going to use that view, you can use this piece of code. That is, by writing binding (the same name you wrote for the instance you created for the class) you can access that view. For example, I created a Listener for this Button and is going to show the user a Toast:

Use view binding in fragments

If you have Fragment instead of Activity, just paste the following in the second step and the description is the same as Activity Description, except we will pass on some other args here:

You can now use the instance of the binding class to reference any of the views:

This was the definition of ViewBinding as well as its use in Activity and Fragment!

Sana Ebadi | 00:00:33 AM , 6 March 2020

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Sana Ebadi
Sana Ebadi

Written by Sana Ebadi

Software Engineer | Trying to make a dream come true| sanaebadi.info

No responses yet

Write a response