Android working with DataBinding

Android DataBinding gives an approach to tie the UI with business rationale permitting the UI esteems to refresh naturally without manual mediation. This decreases part of standard code in your business rationale that you more often than not write to match up the UI when new information is accessible. DataBinding is one of the android engineering parts recommended by android.

In this article we will get familiar with the essentials of DataBinding and in the following article, a straightforward profile screen is manufactured utilizing the RecyclerView with DataBinding.

1. DataBinding Basics

1.1 Enabling DataBinding

To begin with DataBinding, you have to empower this component in your android venture first. Open the build.gradle situated under application and empower dataBinding under android module. Once empowered, Sync the task and you are a great idea to go.

android {

dataBinding {

empowered = genuine

}

compileSdkVersion 27

defaultConfig {

applicationId “info.androidhive.databinding”

minSdkVersion 16

/..

}

}

1.2 Basic Example

Suppose we need to show client data from a User POJO class. We for the most part show the data in a TextView utilizing setText() strategy. In any case, rather than physically calling setText for every client property, DataBinding enables us to tie the qualities consequently.

The underneath POJO class makes a User object with name and email.

open class User {

String name;

String email;

open String getName() {

return name;

}

open void setName(String name) {

this.name = name;

}

open String getEmail() {

return email;

}

open void setEmail(String email) {

this.email = email;

}

}

To empower DataBinding in a design, the root component should begin with <layout> tag. Alongside it, <data> and <variable> labels are utilized.

The following is the structure of information restricting format.

<layout …>

<data>

<variable

name=”…”

type=”…”/>

</data>

<LinearLayout …>

<!- – YOUR LAYOUT HERE – >

</LinearLayout>

</layout>

The format ought to have <layout> as root component. Inside <layout> the typical code of format will be put.

A <data> tag pursues the <layout>. All the coupling factors and techniques ought to go inside <data> tag.

Inside <data> labels, a variable will be proclaimed utilizing <variable> tag. The variable label takes two characteristics ‘name’ and ‘type’. name trait will be nom de plume name and type ought to be of item show class. For our situation, way to User class.

To tie an esteem, @ comment ought to be utilized. In the beneath design, client name and email are bound to TextView utilizing @{user.name} and @{user.email}

<?xml version=”1.0″ encoding=”utf-8″?>

<layout xmlns:android=”http://schemas.android.com/apk/res/android”>

<data>

<variable

name=”user”

type=”info.androidhive.databinding.User”/>

</data>

<LinearLayout xmlns:app=”http://schemas.android.com/apk/res-auto”

xmlns:tools=”http://schemas.android.com/instruments”

android:layout_width=”match_parent”

android:layout_height=”match_parent”

android:orientation=”vertical”

android:padding=”@dimen/fab_margin”

app:layout_behavior=”@string/appbar_scrolling_view_behavior”

tools:context=”.MainActivity”

tools:showIn=”@layout/activity_main”>

<TextView

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:text=”@{user.name}”/>

<TextView

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:text=”@{user.email}”/>

</LinearLayout>

</layout>

When information restricting is coordinated in format document, goto Build – > Clean Project and Build – > Rebuild Project. This will create essential restricting classes.

The produced restricting classes pursues the naming tradition considering the design record name in which restricting is empowered. For the design activity_main.xml, the created restricting class will be ActivityMainBinding (Binding addition will be included toward the end).

To tie the information in UI, you have to expand the coupling design first utilizing the produced restricting classes. Beneath, ActivityMainBinding expands the design first and binding.setUser() ties the User article to format.

You can see here, we haven’t utilized findViewById() anyplace.

import android.databinding.DataBindingUtil;

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import info.androidhive.databinding.databinding.ActivityMainBinding;

open class MainActivity broadens AppCompatActivity {

private User client;

@Override

ensured void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

/setContentView(R.layout.activity_main);

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

client = new User();

user.setName(“Ravi Tamada”);

user.setEmail(“ravi@androidhive.info”);

binding.setUser(user);

}

}

On the off chance that you run the application, you can see the client subtleties showed in TextViews.

android-information restricting precedent

1.3 My DataBinding classes are not created?

Current form of Android Studio neglects to produce the coupling classes the vast majority of the occasions. Typically this can be settled by Cleaning and Rebuilding the task. On the off chance that the issue still perseveres, goto File ⇒ Invalidate Caches and Restart. This ought to conceivably take care of the issue if your design documents are not having any blunders.

1.4 DataBinding in <include> designs

As we can utilize, we have excluded CoordinatorLayout, AppBarLayout and different components in the above model. More often than not, we separate the primary design and substance in two distinct formats i.e activity_main.xml and content_main.xml. The content_main will be incorporated into fundamental format utilizing <include> tag. Presently we’ll perceive how to empower information restricting when we have incorporate designs.

Beneath, we have activity_main.xml with CoordinatorLayout, AppBarLayout and FAB.

The <layout> tag is utilized in activity_main.xml design to empower information authoritative. Also, the <data> and <variable> labels are utilized to tie the User object.

To pass the client to included content_main format, bind:user=”@{user}” is utilized. Without this, the client object won’t be open in content_main design.

<?xml version=”1.0″ encoding=”utf-8″?>

<layout xmlns:bind=”http://schemas.android.com/apk/res/android”>

<data>

<variable

name=”user”

type=”info.androidhive.databinding.User”/>

</data>

<android.support.design.widget.CoordinatorLayout xmlns:android=”http://schemas.android.com/apk/res/android”

xmlns:app=”http://schemas.android.com/apk/res-auto”

xmlns:tools=”http://schemas.android.com/apparatuses”

android:layout_width=”match_parent”

android:layout_height=”match_parent”

tools:context=”.MainActivity”>

<android.support.design.widget.AppBarLayout

android:layout_width=”match_parent”

android:layout_height=”wrap_content”

android:theme=”@style/AppTheme.AppBarOverlay”>

<android.support.v7.widget.Toolbar

android:id=”@+id/toolbar”

android:layout_width=”match_parent”

android:layout_height=”?attr/actionBarSize”

android:background=”?attr/colorPrimary”

app:popupTheme=”@style/AppTheme.PopupOverlay”/>

</android.support.design.widget.AppBarLayout>

<include

android:id=”@+id/content”

layout=”@layout/content_main”

bind:user=”@{user}”/>

<android.support.design.widget.FloatingActionButton

android:id=”@+id/fab”

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:layout_gravity=”bottom|end”

android:layout_margin=”@dimen/fab_margin”

app:srcCompat=”@android:drawable/ic_dialog_email”/>

</android.support.design.widget.CoordinatorLayout>

</layout>

The content_main.xml again incorporates <layout> tag to empower the information official. The <layout>, <data> and <variable> labels are essential in both parent and included formats.

android:text=”@{user.name}” and android:text=”@{user.email}” credits are utilized to show the information in TextViews.

<?xml version=”1.0″ encoding=”utf-8″?>

<layout xmlns:android=”http://schemas.android.com/apk/res/android”>

<data>

<variable

name=”user”

type=”info.androidhive.databinding.User”/>

</data>

<LinearLayout xmlns:app=”http://schemas.android.com/apk/res-auto”

xmlns:tools=”http://schemas.android.com/devices”

android:layout_width=”match_parent”

android:layout_height=”match_parent”

android:orientation=”vertical”

android:padding=”@dimen/fab_margin”

app:layout_behavior=”@string/appbar_scrolling_view_behavior”

tools:context=”.MainActivity”

tools:showIn=”@layout/activity_main”>

<TextView

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:text=”@{user.name}”/>

<TextView

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:text=”@{user.email}”/>

</LinearLayout>

</layout>

open class MainActivity broadens AppCompatActivity {

@Override

ensured void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

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

setSupportActionBar(binding.toolbar

Leave a Reply

Your email address will not be published. Required fields are marked *