Card image cap

How to usee View Binding in Android studio 3.6+ | Java | Androidx | codingWithMitch

Why we need View Binding?

Use to new View Binding library to replace findViewById in your app. It let you write a safe and concise code. It is less boilerplate. Instead of lookup every view, it generates a binding object that holds all the views for you. View Binding adds safety to your code. Whenever you inflate a view binding object will give you the automatically right type of that view. And if the view is only present in the landscape, then the binding will be nullable. This helps you to prevent crashing in production.

View binding required android studio 3.6 or higher, android Gradle plugin 3.6+. To use in your module enable it in your build.gradle file. you don't need to include any library to use view binding. when you enable the compiler it will automatically recompile your project and generate binding objects for every layout. so if you have a layout file called activity_main.xml View binding generate a small class called ActivityMainBinding. It will have one correctly typed and null safe property for every view with an Id in the layout. Then in the activity instead of inflating the layout directly, you can use the inflate method on ActivityMainBinding. It will inflate the layout for you and return a binding object with all views bound. The layout for your activity is stored in a special property called root. so you can call setContentView(binding.root). And that is all you need to know to get started with view binding. It is really simple, safer, and more concise then FindViewById.

What you need for View Binding:

  • Android studio 3.6 or higher
  • Android Gradle plugin 3.6 or higher

Which language you can use for this:

  • java
  • kotlin

 

Table of Contents

  1. Step 1: Create a New Project
  2. Step 2: Build.gradle (app)
  3. Step 3: Design the layout
  4. Step 4: Bind your view in MainActivity.java
  5. Step 5: Use any component directly without "findViewById"


Beginning of the tutorial:

 Step 1: Create a New Project

First of All, create a new android studio project by clicking on File -> New -> New Project.



Step 2: Build.gradle (app)

Next, go to the Build.gradle (app) level and enable the view Binding by inserting the below code in android{....} block and click sync Now



Step 3: Design the layout.

Now go to activity_main.xml which is the default activity and Enter some code according to your need. I am adding just an EditText and Button for the sake of simplicity.

Below is the complete code of activity.

activity_main.xml



Step 4: Bind your view in MainActivity.java

Now go to the MainAcitvity.java File.

Afterward, you can see an automatic class is automatically imported in your project called "ActivityMainBinding". Just Import this class.



 

Next, remove the below code from your MainActivity.java

Next, implement the below code in the onCreate method.


Step 5: Use any component directly without "findViewById"


Now you can use any componenat directly without accessing the actual "findViewById"

Just like code below.

Here is the complete code of the activity.

MainActivity.java

Now just run the app and app works fine.
Want to learn more check out the 
  official documentation



Authors


Muhammad Arslan

fb.com/arslan6015

android developer, Technical content writer


Create an Account



Have an account? Log In

CodingWithMitch Members

Unlimited access to all courses and videos

Step by step guides to build real projects

Video downloads for offline viewing

Members can vote on what kind of content they want to see

Access to a private chat with other communnity members & Mitch

Become a Member

CodingWithMitch Members

Unlimited access to all courses and videos

Step by step guides to build real projects

Video downloads for offline viewing

Members can vote on what kind of content they want to see

Access to a private chat with other communnity members & Mitch

Become a Member

Comments