20 Oct 2015

Data Binding in Android Marshmallow 6.0

Hello Friends,

There are lots of exiting library come up with new android feature.
One of the new API that caught my eye is the Data Binding Library. In short, it lets you bind data directly into your layouts by having a POJO - variable declaration pair.

Key features:

  • The Data Binding Library offers both flexibility and broad compatibility — it's a support library, so you can use it with all Android platform versions back to Android 2.1 (API level 7+).
  • So far so good, nothing really complicated or hard to comprehend. In a way, this removes a bit of boilerplate code (not having to findViewById() or setText()). My first impression was that this is the aim of this API.

Build Environment :


1) The Data Binding plugin requires Android Plugin for Gradle 1.3.0-beta4 or higher, so update your build dependencies (in the top-level build.gradle file) as needed.

2) Make sure you are using a compatible version of Android Studio. Android Studio 1.3 adds the code-completion and layout-preview support for data binding.

Setting Up Work Environment:

Step 1 :
To set up your application to use data binding, add data binding to the class path of your top-level       build.gradle file, right below "android".

dependencies {
    classpath 'com.android.tools.build:gradle:1.3.0'
    classpath "com.android.databinding:dataBinder:1.0-rc1"
}
Then make sure jcenter is in the repositories list for your projects in the top-level build.gradle file.

Step 2 :
In each module you want to use data binding, apply the plugin right after android plugin

apply plugin: 'com.android.application'
apply plugin: 'com.android.databinding'


Let's take the example  to understand better :


User.java :

public class User {
    public final String firstName;
    public final String lastName;
    public User(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
}

In the layout in which we want to display the user's first and last name. With Data Binding we can directly reference the user's fields in the layout

activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android = "http://schemas.android.com/apk/res/android">
    <data>
        <variable name = "user" type = "com.testapp.User"/>        
    </data>

    <LinearLayout
        android:layout_width = "match_parent"
        android:layout_height = "match_parent"
        android:orientation = "vertical" >

        <TextView android:textColor="@android:color/black"
            android:layout_width = "wrap_content"
            android:layout_height = "wrap_content"
            android:text = "@{user.firstName}"/>

        <TextView
            android:textColor="@android:color/black"
            android:layout_width = "wrap_content"
            android:layout_height = "wrap_content"
            android:text = "@{user.lastName}"/>   
    </LinearLayout>
</layout>

MainActivity.java :

A binding class will be generated based on the name of the layout file (in this case ActivityMainBinding) which you can use in your activity to actually tell the layout which user object to use

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

 user user=new user("Hello"," World !!!");
 binding.setUser(user);

*****Binding Events*****

Events may be bound to handler methods directly, similar to the way android:onClick can be assigned to a method in the Activity.

Step 1: Create MyHandlers.java class 
You can add method to perform action on button click same what we did before like onClickListner

public class MyHandlers
{
    public void onClickButton(View view)
    {
        Toast.makeText(view.getContext(),"Button press",Toast.LENGTH_SHORT).show();
        Log.i(MyHandlers.class.getSimpleName(), "Button press...");
    }
}

Step 2 : Modify activity_main.xml
- Add one more variable inside the <data> tag Eg.
<variable name = "clickHandler" type = "com.testapp.MyHandlers"/> 

 Add button inside the xml file , bind the button with its click even like as below

<Button
    android:layout_width = "wrap_content"
    android:layout_height = "wrap_content"
    android:text = "New Button"
    android:id = "@+id/button"
    android:onClick="@{clickHandler.onClickButton}"/>
We can perform many action using Bind data feature. Please refer following links
Data binding Operation 

I will be happy if you will provide your feedback or follow this blog. Any suggestion and help will be appreciated.
Thank you :)

1 comment:

  1. Can you get multiline code ? I know it should be short, but multiline is more readable than one long line... (in the "@{...|" part.

    ReplyDelete