Monday, 9 September 2019

Android Data Binding Updates

Hello Guys i m updating regarding Data Binding features given in  Android Jetpack libraries

Here how we can implement Data Binding in Android its given.

-> This Library allows bind Layouts components with data in xml declaration instead of bind data and ui controls programmatically.

->  Example use the Data Binding  to assign textView in Xml layout.

->  No need to call other Java code

->  use this syntax @{} in assignment of control

Like

<TextView
    android:text="@{pojomodel.userName}" />

do in build.gradle file

android {
...
dataBinding {
    enabled = true}
}

->  in Details under stand like below
-> Create Pojo class.

package com.t.databinding;

public class Pojomodel {

    private String mid;
    private String strname;


    public String getMid() {
        return mid;
    }

    public void setMid(String mid) {
        this.mid = mid;
    }

    public Pojomodel(String id, String name) {
        this.mid = id;
        this.strname = name;
    }

    public String getName() {
        return strname;
    }

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

}


-> Create Layout file like


<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <!--Step 1-->
    <data>
        <!--Step 2-->
        <variable
            name="Pojomodel"
            type="com.t.databinding.Pojomodel" />
    </data>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="16dp"
        tools:context="com.androidbytes.databindingdemo.MainActivity">
        <!--Step 3-->
        <TextView
            android:id="@+id/text_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{Pojomodel.mid}"
            android:textColor="@android:color/black"
            android:textSize="26sp" />
        <TextView
            android:id="@+id/text_view_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{Pojomodel.name}"
            android:textColor="@android:color/black"
            android:textSize="18sp" />
    </LinearLayout>
</layout>

-> Create  MainActivity

package com.t.databinding;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import com.t.databinding.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {

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

        //        After Data Binding
        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        Pojomodel personVO = new Pojomodel("#1","Abc ");
        binding.setPojomodel(personVO);

    }
}


-> Data Binding  Use in  Application improve app performance , handle null pointer exception,  
    prevent from memory leak
-> Thanks this detail will help full to you for understand about Data Binding usage in Android Application.





No comments:

Post a Comment

Comments

Find Hours Diffrence in Kotlin

  In Kotlin, determining the difference in hours between two timestamps is a common task, especially in scenarios involving time-based calcu...