Android Data Binding — A cool binding tool

Sakhawat Hossain
3 min readDec 28, 2019

--

Ever since android development started I guess everyone had a problem with findViewById() thing. Whenever you add a new component in your XML, you need to add findViewById() in order to make that component functional from your java class.

Think about, you are making a register where you have 30 components in which some are text views, some are edit texts, etc. Again in order to make those 30 components fully functionalble, you have to add respective findViewById(), which means this findViewById() takes 30 more extra LOC(Lines Of Code) and makes your code less efficient.

There are a couple of ways by which you can avoid findViewById() issues.Following are some of the libraries that can help us with data binding

https://github.com/JakeWharton/butterknife

These are some of the popular libraries that have been using by developers. But today I am going to talk about a simple one and which is the dataBinding tool. I have added an example of how that works, so let's start.

Step 1: Create a new project.

Step 2: Open your build.gradle(app) and add dataBinding like the following one:

apply plugin: 'com.android.application'

android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.shakil.databinding"
minSdkVersion 15
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
dataBinding.enabled = true
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}

The main part here was to add dataBinding.enabled = true this simple line. After you have done that hit the Sync button.

Step 3: Wrap your layout file into the layout tag like the following.

<?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"
>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
>

<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_margin="8dp"
android:layout_gravity="center"
/>

<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_margin="8dp"
android:layout_gravity="center"
/>

<TextView
android:id="@+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_margin="8dp"
android:layout_gravity="center"
/>

</LinearLayout>

</layout>

After you have done that rebuild your project.dataBinding works in runtime, it created another binding class at runtime in order to bind data. The naming convention goes along with the layout file name. As an example your layout file name is activity_main.xml, so the generated file name will be as ActivityMainBinding.Now let's move on to your java class.

public class MainActivity extends AppCompatActivity {

private ActivityMainBinding activityMainBinding;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
}
}

Here you will be able to see that instead of setContentView we use that instance of Binding class which generated at runtime based on our layout file name and which later set the view with the help of DataBindingUtil class. Now you can access any components into your XML file without findViewById(), you just have to use the instance of binding class like the following sample.

activityMainBinding.tv1.setText("Whooooo !!! Its working");
activityMainBinding.tv2.setText("Whooooo !!! Its working");
activityMainBinding.tv3.setText("Whooooo !!! Its working");

Here tv1,tv2,tv3 are the id of the textViews that I used into my example layout file.

Thanks for reading. That was for today. Wish you a very good life!

Claps and share will be much appreciated.

Happy Coding

You can find the example project into the following link.

https://github.com/shakiz/DataBinding

--

--

Sakhawat Hossain
Sakhawat Hossain

Written by Sakhawat Hossain

Senior Software Engineer | Android | iOS | Java | Kotlin | Flutter

No responses yet