Concept of View Binding in Android
We’ll go through view 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 view binding; but, if you don’t, you will by the conclusion of this article!😉
Okay, why we need View 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 view binding, how can you apply it in your next billion-dollar app?
Another question how you can use view binding😶?
You must first enable data binding in your project before proceeding (View binding is the sub-class of data binding). 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 view 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 lifecycle 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 view 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 View 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 🚀