Mystery behind ViewBinding and DataBinding (Part I)

Abhishek
4 min readFeb 4, 2024

--

Data binding is an extension of the View binding feature. So, in Part I of the article we will cover the View binding implementation and working details, and in Part II we will cover the Data binding.

ViewBinding

View binding is a feature that makes it easier to write code that interacts with views. Once view binding is enabled in a module, it generates a binding class for each XML layout file present in that module. An instance of a binding class contains direct references to all views that have an ID in the corresponding layout.

In most cases, view binding replaces findViewById.

Implementation

Step 1: Enable ViewBinding in the app-level gradle file

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

Step 2: Create a layout file and add the required view with the IDs (ref will be generated only for the view which has the id)

<!-- file name = activity_main.xml -->
<LinearLayout ... >
<TextView android:id="@+id/name" />
<Button android:id="@+id/button" />
</LinearLayout>

Step 3: Using the generated files

// layout_file.xml will have ref with name LayoutFileBinding. 
// So, in our case layout file name is activity_main.xml which is generated as ActivityMainBinding
private lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
// Get View Ref with binding.viewId
binding.name.text = viewModel.name
}

We have enabled the View binding in our project but what happens when we enable the View binding? And, how the layout view reference is available for use in the Kotlin side of the code?

We will answer these questions in the next section.

Internal Working

What happens when we enable the View binding?

To answer this question, Run the ./gradlew assemble --dry-run command in the terminal which will give us a list of tasks. In the list, we have some binding-related tasks (listed below) whose main purpose is to generate the reference classes from the layout files.

// For Debug build
:app:dataBindingMergeDependencyArtifactsDebug SKIPPED
:app:dataBindingGenBaseClassesDebug SKIPPED

// For Release build
:app:dataBindingMergeDependencyArtifactsRelease SKIPPED
:app:dataBindingGenBaseClassesRelease SKIPPED

After running, ./gradlew dataBindingGenBaseClassesDebug we will have class references for our layout files in the build folder under debug (as we have executed the debug build command)

So, this is the class that we are using in Step 3 of implementation as a binding reference for the activity_main.xml layout.

But when and how View references are added in the generated class?

To understand this, let’s go back to step 3 of our Activity code where we are calling ActivityMainBinding.inflate(layoutInflater) .When we call the static method inflate() from the ActivityMainBinding class, it internally creates the root view using the inflater which is passed as a parameter and attachToParent will be false in our case as we are calling from Activity.

After creating the root view, it calls the bind() method which is part of the same class. If we look at the code of the bind() method, we will see it creates the reference of view using ViewBindings.findChildViewById() and returns the reference of ActivityMainBinding with the view reference in the constructor.

Lastly in the constructor, it just adds the view reference in the global variable which we are using to get the reference of the view.

Important Notes:

  1. All generated class extends the ViewBinding interface which has one method named getRoot() to return the root view. So, all generated classes override this method to return the root view.
  2. If we have a nested layout file, like if one layout file includes another layout file then both layout files will have a different generated class but the parent generated class will have a reference to the child layout generated class. Similar to the below snippet
activity_main.xml
NestedLayoutBinding.java
ActivityMainBinding.java

Check the View binding documentation for more usage information.

Check the next part, where we have covered the implementation and working of Data binding.

Thanks for reading. Happy coding!

--

--

No responses yet