Android Data Binding || data binding in android

 

In this blog on databinding we will get to know the usage of view binding’s, every app we use different types of UI components and we programmatically declare them before we use. It’s a general scenario we do follow but as a better practice we have data binding library’s.

Before getting any further we need to declare data binding in our project

 

Databinding

To make use of databinding in your app

build.gradle(Module: app)

android {

    dataBinding {

        enabled = true;

    }
}

 

 

Databinding library’s change the way we declare our UI components. Let’s see a example of a TextView declaration,

 

TextView textView = (TextView) findViewById(R.id.txt_student_name);

TextView textView = findViewById(R.id.txt_student_name);

a textview can be declared in either of the two ways.

According to the databinding library’s provided by Android as a part of jetpack we can skip the above process and do declarations within the layout file by wrapping our layout in

<layout>
    
</layout>

 

inside layout declare data field

<data>
   
</data>

 

using this data field add a variable declaration field

<variable
    name="student"
    type="com.androidcoding.databinding.Student" />

 

Here the student is a viewmodel class

 Student.class:

package com.androidcoding.databinding;

public class Student {

    String name;
    String email;
    String btnName;


    public String getName() {
        return name;
    }

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

    public String getEmail() {
        return email;
    }

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

    public String getBtnName() {
        return btnName;
    }

    public void setBtnName(String btnName) {
        this.btnName = btnName;
    }
}

 

Now comes the layout section i.e, declaring our text view and making it work with our view model class.

<TextView
    android:id="@+id/txt_student_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@{student.name}"/>

 

we assigned the text field to view model class name key.

android:text="@{student.name}

 

here student is the name of the variable we have declared

<variable
    name="student"
    type="com.androidcoding.databinding.Student" />

 

in the same way we declare another textfield email address

 

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

 

MainActivity.class:

In the main activity file we need to declare our viewmodel.

private Student student;

Generally we declare layout in our activity as

setContentView(R.layout.activity_main);

 

but as we are using data binding we can skip the above step and bind our layout so that we can use the UI components directly from layout without any individual declarations.

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

 

using our viewmodel class we can assign data to our textview as

 

student = new Student();

student.setName("Abhi");

 

now set viewmodel class to our binding object.

binding.setStudent(student);

 

Complete Code:

activity_main.xml

 

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

    <data>
        <variable
            name="student"
            type="com.androidcoding.databinding.Student" />
    </data>

    <LinearLayout
        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"
        tools:context=".MainActivity"
        android:gravity="center"
        android:orientation="vertical">

        <TextView
            android:id="@+id/txt_student_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{student.name}"/>

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

    </LinearLayout>


</layout>

 

MainActivity.Class

 

package com.androidcoding.databinding;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.androidcoding.databinding.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {

    private Student student;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        student = new Student();

        student.setName("Abhi");
        student.setEmail("abhi@gmail.com");
        student.setBtnName("Button");

        binding.setStudent(student);


    }
}

 

Output:

The output screen depicting databinding in the android

databinding

 

In this way we achieve setting data to textviews.But here comes the second question how can we manage ‘onClick’.

In the next tutorial we will be dealing with onClick handlers using databinding.

 

If you are having any query’s in this tutorial on databinding do let us know in the comment section below.If you like this tutorial do like and share us for more interesting updates.

 

Show Buttons
Hide Buttons
Read previous post:
Flutter Alert Box Library || Customized Alert Dialog

  Flutter alert box, Flutter a new way to program has been optimizing the way you code individually for android...

Close