Concept of Data Binding in Android
We’ll go through data binding in detail, including why we need it and why any Android developer should have it on their resume.
If you’ve come this far, it’s safe to assume you know something about data binding; but, if you don’t, you will by the conclusion of this article!😉
Okay, why we need Data binding 🤔?
To put it another way, we use it to improve the performance of our cool application. We used to use this obnoxious syntax called “findViewById” in the stone age, which basically traverses through views anytime we use it (it’s basically like running any query in a database again and over), which may be a major issue in some extremely large projects. Because it does not allow runtime compilation, it can also result in a null pointer error.
So now that you understand why we utilize data binding, how can you apply it in your next billion-dollar app?
Another question how you can use data binding😶?
You must first enable data binding in your project before proceeding. You must write the following code in the android block of your project’s build.gradle file:
android {
compileSdkVersion 30
buildToolsVersion "29.0.3" ------------------------
buildFeatures {
dataBinding = true
}
}
The second step is to surround the entire XML code with a layout tag, as follows:
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/name_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
------------------
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
After that, the data binding library creates a binding object instance in the following way:

Then, in the kotlin file, late initialize a variable binding with an instance of your binding object, and in the OnCreate method, we’ll initialize this binding variable by setting it to the supplied layout by substituting setContentView:
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this,R.layout.activity_main)
}
}
As a result, you now have the ability to use data binding in your project. By just calling binding.id, you can reach any view.
binding.buttonid.setOnClickListener {
____________________
}
binding.textid.text = "message"
If you don’t want to use the phrase binding all the time, you can use the term like binding.apply{} instead, and you may call any id and perform your actions within that scope:
binding.apply {
buttonid.setOnClickListener {
______________
}
textid.text = "message"
}
So revise it:
1. Enable Data Binding
2. Wrap XML code in <layout> your code </layout>.
3. Create Binding instance in kotlin file.
4. Use binding.id to get instance of any view.
Bye bye findViewById 🚀