Friday 14 September 2018

HB Blog 159: Jetpack Architecture: - DataBinding.

The Data Binding Library is a support library that allows you to bind UI components in your layouts to data sources in your app using a declarative format rather than programmatically.

The expression language allows you to write expressions that connect variables to the views in the layout. The Data Binding Library automatically generates the classes required to bind the views in the layout with your data objects. The library provides features such as imports, variables, and includes that you can use in your layouts.

These features of the library coexist seamlessly with your existing layouts. For example, the binding variables that can be used in expressions are defined inside a data element that is a sibling of the UI layout's root element.
The Data Binding Library generates binding classes that are used to access the layout's variables and views

Refer the below link for complete sample code:-

Download Sample Code

Have a look on few code snippets,

//build.gradle
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    buildToolsVersion '27.0.3'
    defaultConfig {
        applicationId "com.harshalbenake.databinding"
        minSdkVersion 16
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    dataBinding {
        enabled true
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation('com.android.support:support-v4:27.1.0')
    implementation 'com.android.support:appcompat-v7:27.1.0'

}

//activity_main.xml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <data>
        <import type="android.view.View"/>
        <import type="com.harshalbenake.databinding.MainActivity.Utils"/>
        <variable
            name="dataPojo"
            type="com.harshalbenake.databinding.DataPojo" />
        <variable name="presenter" type="com.harshalbenake.databinding.Presenter" />

    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="16dp"
        android:gravity="center"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{Utils.appendSurname(dataPojo.name),default=nameXML}" />

        <TextView
            android:id="@+id/tv_email"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{dataPojo.email,default=emailXML}" />

        <Button
            android:layout_width="wrap_content"
            android:text="HBBlogs"
            android:layout_gravity="center"
            android:layout_height="wrap_content"
            android:onClick="@{(view) -> presenter.onSaveClick(view,dataPojo)}" />
    </LinearLayout>
</layout>

//DataPojo.java 
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
package com.harshalbenake.databinding;

public class DataPojo{
    private final String name;
    private final String email;

    public DataPojo(String name, String email) {
        this.name = name;
        this.email = email;
    }

    public String getName() {
        return name;
    }

    public String getEmail() {
        return email;
    }
}

//MainActivity.java 
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.harshalbenake.databinding;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import com.harshalbenake.databinding.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity implements Presenter{
    private int mCounter=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Inflate the content view (replacing `setContentView`)
        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        //adding data to pojoobject
        DataPojo dataPojo=new DataPojo("Harshal","harshalbenake@gmail.com");
        //setting datapojo to binding
        binding.setDataPojo(dataPojo);
        //set listner to9 binding
        binding.setPresenter(this);
        //setting textview appearance without creating textview object
        binding.tvName.setTextSize(25f);
    }

    @Override
    public void onSaveClick(View view, DataPojo dataPojo) {
        mCounter=mCounter+1;
        Toast.makeText(this,"Name: "+dataPojo.getName()+"\nEmail: "+dataPojo.getEmail()+"\n"+mCounter,Toast.LENGTH_SHORT).show();
    }


    /**
     * Inner static class Utils
     */
    public static class Utils {
        Context mContext;
        public Utils(Context context) {
            this.mContext=context;
        }

        /**
         * appends Surname
         * @param strText
         * @return
         */
        public static String appendSurname(String strText){
            return strText+" Benake";
        }
    }

}