Android Databinding Library || java || jet pack

 

Android dataBinding :

Android databinding, Jetpack provides new data binding mechanisms using which a user can bind UI components in layouts to data sources directly.

Databinding also avoids boiler plate code in handling the UI components.Also binding helps you to avoid UI framework calls in activity there by making app performance much better.

Also we can avoid memory leaks and null pointer exception by using android databinding.

When you declare a data binding to a layout file then all the views inside the layout including another layout which are add using include tag can also be bonded.

 

Dependency :

No need to add new dependencies jut make databinding enable.

android {
.
.
.
   dataBinding{
    enabled = true
   }

}

 

The android databinding library generates binding classes by using which we can access views and variables declared in the layout file.

These databinding library’s can also be used with the architecture components which make the UI design much robust and easy for testing scenarios.

 

Handling textview previously

TextView txtView = findViewById(R.id.txtView);
txtView.setText("Text");

 

With android dataBinding

Setting text to textView as  ->  android:text=”@{User.name}

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@{User.name}"/>

 

and in class file

user.setText("");

its simplified version of declaration for full information go through entire tutorial.

 

activity_main.xml

In layout level declare binding as below so as to generate ActivityMainBinding class

<layout>

    <data>

        <variable
            name="User"
            type="com.abhi.databinding_java.User" />
    </data>
    .
    .
    .
    
</layout>

 

the format of layout file is

<layout>

    <data>

        .
        .
        .
    </data>


    <RelativeLayout 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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        tools:context=".MainActivity">

        .
        .
        .
        
    </RelativeLayout>

</layout>

 

now adding up textview where we bind the data directly to the view using User model class

 

<?xml version="1.0" encoding="utf-8"?>

<layout>

    <data>

        <variable
            name="User"
            type="com.abhi.databinding_java.User" />
    </data>


    <RelativeLayout 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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        tools:context=".MainActivity">

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:layout_centerHorizontal="true"
            android:textColor="@android:color/black"
            android:text="@{User.name}"/>

        <TextView
            android:id="@+id/age"
            android:layout_below="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:layout_centerHorizontal="true"
            android:textColor="@android:color/black"
            android:text="@{User.age}" />

        <TextView
            android:id="@+id/mobile"
            android:layout_below="@+id/age"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:layout_centerHorizontal="true"
            android:textColor="@android:color/black"
            android:text="@{User.mobile}" />

        <TextView
            android:id="@+id/email"
            android:layout_below="@id/mobile"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:layout_centerHorizontal="true"
            android:textColor="@android:color/black"
            android:text="@{User.email}" />

    </RelativeLayout>

</layout>

 

User.java

A data class to handle the textviews to set and get data.

package com.abhi.databinding_java;

public class User {

    public String name;
    public String age;
    public String mobile;
    public String email;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

MainActivity.java

Declare android dataBinding class in main activity after layout declarations.

Note: layout declaration is must for DataBindingClass to be generated.

If you are facing problem with databinding class generation then try to reopen android studio by performing invalidate cache restart.

 

ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this,R.layout.activity_main);

 

Initialize model class to handle the data setters and getters

User user = new User()

 

now you can set text as below to respective textviews

 

user.setName("Abhi");
user.setAge("26");
user.setMobile("9876543210");
user.setEmail("abhi@email.com");

 

and finally need to add data class to binding class

activityMainBinding.setUser(user);
public class MainActivity extends AppCompatActivity {


    ActivityMainBinding activityMainBinding;

    User user = new User();

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

        user.setName("Abhi");
        user.setAge("26");
        user.setMobile("9876543210");
        user.setEmail("abhi@email.com");

        activityMainBinding.setUser(user);


    }
}

 

Output :

This screen depicts the android databinding.

 

android databinding

 

 

Show Buttons
Hide Buttons
Read previous post:
Async Task Example || Android Tutorial

  When ever we try to implement a background task we need to implement it properly using a async task....

Close