Introduction:
Android Data binding is one of the hot topic among Android developers ever since its first launch in Mid 2015, helping to build the apps faster.
This Article explains about the same, Data binding Library concept and how to use it in android using Kotlin. Data Binding not only supports in eliminating boilerplate code but also helps in faster development times and faster execution times.
Advantages:
- It eliminates boilerplate code (like findViewById)
- It provides a stronger readability, it would be easy to learn about it.
- Provides support to separate UI logic and business logic.
Similar Links: How to Read a File using Kotlin
Let’s see how to implement data binding in our application.
Configuring Android Data Binding:
To enable the usage of data binding in your app, first edit your application’s build.gradle file and add the following code snippet.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dataBinding | |
{ | |
enabled = true | |
} |
Data Binding Layout File:
Generally, in default layout files we have ViewGroup(E.g:LinearLayout, RelativeLayout) as parent tag. But, in this case to support data binding techniques, we must have <layout> tag as root tag. For a MainActivity class, a activity_main.xml layout would be like this:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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"> | |
<android.support.constraint.ConstraintLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context="com.processmap.databindingexample.MainActivity"> | |
<TextView | |
android:id="@+id/txtView" | |
android:layout_width="wrap_content" | |
android:layout_height="14dp" | |
android:text="Write Text Here" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintLeft_toLeftOf="parent" | |
app:layout_constraintRight_toRightOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
</android.support.constraint.ConstraintLayout> | |
</layout> |
Actual view (here it is Constraint layout) is defined within layout tag to support data binding techniques.The layout tag is a tag which indicates build system that, this file should be processed for data binding. Any layout file is without that tag , will not support data binding.
Data Binding Activity:
After this, binding class will be auto generated based on the name of layout file by default.To associate it in your class, invoke setContentView() using DataBindingUtil like this:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.databinding.DataBindingUtil | |
import android.support.v7.app.AppCompatActivity | |
import android.os.Bundle | |
import com.processmap.databindingexample.databinding.ActivityMainBinding | |
class MainActivity : AppCompatActivity() { | |
private var mBinding: ActivityMainBinding? = null //This ActivityMainBinding class is auto generated by data binding library for us | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
mBinding=DataBindingUtil.setContentView(this,R.layout.activity_main) //Setting contentview usind data binding. | |
mBinding?.txtView?.setText("Hello!") //Accessing textview using mbinding | |
} | |
} |
That’s it, we are done. Run the project and check output.
References: Data Binding Library implementation