Post date: Feb 23, 2016 9:49:48 AM
Data binding is an excellent feature introduced in android to implement real MVVM pattern in mobile application developments.
1. Enable data binding
Open build.gradle and add following configuration in it.
android {
.....
dataBinding{
enabled = true
}
.....
}
A POJO class which is extent from BaseObservable object is called as observable object which can be directly bind to any xml files in android
Getter method of each property fields in this POJO class should have @Bindable annotation
Setter method of each property field in the POJO class should notify the property change through "notifyPropertyChanged" method.
"BR" is a auto generated enum for each properity fields which has @Bindable annotation
package bob.bindme.model;
import android.databinding.BaseObservable;
import android.databinding.Bindable;
import bob.bindme.BR;
/**
* Created by boobalanmunusamy on 2/23/16.
*/
public class CEmployee extends BaseObservable {
private String userName;
private String password;
private String address;
private String emailAddress;
@Bindable
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
notifyPropertyChanged(BR.userName);
}
@Bindable
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
notifyPropertyChanged(BR.password);
}
@Bindable
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
notifyPropertyChanged(BR.address);
}
@Bindable
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
notifyPropertyChanged(BR.emailAddress);
}
}
2. Move default activity xml codes inside the <layout></layout> tag
Normal Activity XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="bob.bindme.LoginActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context="bob.bindme.LoginActivity"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_weight=".3"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="User Name" android:padding="10dp"
android:id="@+id/textView" />
<EditText
android:layout_width="0dp"
android:layout_weight=".7"
android:layout_height="wrap_content"
android:id="@+id/etUserName" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="0dp"
android:layout_weight=".3"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Password"
android:padding="10dp"
android:id="@+id/txtPassword" />
<EditText
android:layout_width="0dp"
android:layout_weight=".7"
android:layout_height="wrap_content"
android:password="true"
android:id="@+id/etPassword" />
</LinearLayout>
<Button
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="wrap_content"
android:text="Login"
android:id="@+id/btnLogin" />
</LinearLayout>
</RelativeLayout>
Updated XML file has been mentioned below;
activity_login.xml
<?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>
<variable
name="employeeInfo"
type="bob.bindme.model.CEmployee"/>
</data>
<RelativeLayout android:layout_width ="match_parent" android:layout_height= "match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".activity.LoginActivity"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_weight=".3"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="User Name" android:padding="10dp"
android:id="@+id/textView" />
<EditText
android:layout_width="0dp"
android:layout_weight=".7"
android:layout_height="wrap_content"
android:text="@{employeeInfo.userName}"
android:id="@+id/etUserName" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="0dp"
android:layout_weight=".3"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Password"
android:padding="10dp"
android:id="@+id/txtPassword" />
<EditText
android:layout_width="0dp"
android:layout_weight=".7"
android:layout_height="wrap_content"
android:password="true"
android:text="@{employeeInfo.password}"
android:id="@+id/etPassword" />
</LinearLayout>
<Button
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="wrap_content"
android:text="Login"
android:onClick="onLoginButtonClicked"
android:id="@+id/btnLogin" />
</LinearLayout>
</RelativeLayout>
</layout>
public class LoginActivity extends AppCompatActivity {
CEmployee cEmployee;
ActivityLoginBinding activityLoginBinding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityLoginBinding = DataBindingUtil.setContentView(this, R.layout.activity_login);
cEmployee = new CEmployee();
activityLoginBinding.setEmployeeInfo(cEmployee);
}
}