Start a Project

Two-way data binding in android

In this blog, we are going to show you how to use two-way data binding for the form validation. But before that, we should know about the Basic Data binding technique.

Data Binding Recap

With the release of Android M , the data binding came into android. As its a support library it supports back to API level 7. The data binding is a technique to bind the application logic to the layouts which were earlier a redundant work.

Data Binding technique android Part-I

How to use the Data binding with view Id,

Data Binding in android with View ID

After going though both of these blogs, now we can continue for two-way data binding.

 

How can we use

Two-way data binding allows you to automatically transfer data from user input back into your data model. If we want to support two-way binding so we have to use “@={expression}” syntax for two-way binding:

In model UserData.java

In your model class, you have to extend the BaseObservable and for notify, you have to use “notifyPropertyChanged(BR.email)” in your setter.

notifyPropertyChanged

Notifies listeners that a specific property has changed. The getter for the property that changes should be marked with Bindable to generate a field inBR to be used as fieldId.

Complete Code

This is the simple login form where we have used the two-way data binding for form validation,

activity_main.xml

 

Model- UserData.java

 

MainActivity.java

 

Thank you for reading it, maybe it will help you to understand the Two-way data binding.

Happy Coding, Stay Super and Stay Cool.

Exit mobile version