Android App Development
iOS App Development
Flutter App Development
Cross Platform App Development
Hire on-demand project developers and turn your idea into working reality.
Big thanks to Webkul and his team for helping get Opencart 3.0.3.7 release ready!
Deniel Kerr
Founder. Opencart
Top Partners
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.
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.
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:
<EditText android:id="@+id/username12" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="30dp" android:hint="Email" android:text="@={userdata.email}" android:inputType="textEmailAddress" android:singleLine="true" android:textColorHint="#9F9F9F" /> 1234567891011 <EditText android:id="@+id/username12" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="30dp" android:hint="Email" android:text="@={userdata.email}" android:inputType="textEmailAddress" android:singleLine="true" android:textColorHint="#9F9F9F" />
In model UserData.java
public class UserData extends BaseObservable { private String email; public UserData(Context context) { this.context = context; } @Bindable public String getEmail() { if (email == null) { return ""; } return email; } public void setEmail(String email) { this.email = email; notifyPropertyChanged(BR.email); } } 1234567891011121314151617181920 public class UserData extends BaseObservable { private String email; public UserData(Context context) { this.context = context; } @Bindable public String getEmail() { if (email == null) { return ""; } return email; } public void setEmail(String email) { this.email = email; notifyPropertyChanged(BR.email); }}
In your model class, you have to extend the BaseObservable and for notify, you have to use “notifyPropertyChanged(BR.email)” in your setter.
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.
Bindable
BR
fieldId
This is the simple login form where we have used the two-way data binding for form validation,
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <data> <variable name="userdata" type="application.coderdna.twowaydatabingingdemo.Model.UserData"></variable> </data> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="30dp"> <RelativeLayout android:id="@+id/login_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentBottom="true" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_gravity="right"> <android.support.design.widget.TextInputLayout android:id="@+id/userlayout11" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" app:error="@{userdata.emailError}" android:layout_alignParentTop="true"> <EditText android:id="@+id/username12" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="30dp" android:hint="Email" android:text="@={userdata.email}" android:inputType="textEmailAddress" android:singleLine="true" android:textColorHint="#9F9F9F" /> </android.support.design.widget.TextInputLayout> <android.support.design.widget.TextInputLayout android:id="@+id/passLayout11" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" app:error="@{userdata.passwordError}" android:layout_below="@+id/userlayout11"> <EditText android:id="@+id/password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@+id/forgot_password" android:hint="Password" android:text="@={userdata.password}" android:inputType="textPassword" android:singleLine="true" android:textColorHint="#9F9F9F" /> </android.support.design.widget.TextInputLayout> <Button android:id="@+id/login" android:layout_width="280dp" android:layout_height="wrap_content" android:layout_below="@+id/passLayout11" android:layout_centerHorizontal="true" android:layout_marginTop="49dp" android:background="@color/colorAccent" android:text="Login" android:onClick="retrofitCall" android:textColor="#FFFFFF" /> </RelativeLayout> </FrameLayout> </RelativeLayout> </layout> 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 <?xml version="1.0" encoding="utf-8"?><layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <data> <variable name="userdata" type="application.coderdna.twowaydatabingingdemo.Model.UserData"></variable> </data> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="30dp"> <RelativeLayout android:id="@+id/login_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentBottom="true" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_gravity="right"> <android.support.design.widget.TextInputLayout android:id="@+id/userlayout11" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" app:error="@{userdata.emailError}" android:layout_alignParentTop="true"> <EditText android:id="@+id/username12" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="30dp" android:hint="Email" android:text="@={userdata.email}" android:inputType="textEmailAddress" android:singleLine="true" android:textColorHint="#9F9F9F" /> </android.support.design.widget.TextInputLayout> <android.support.design.widget.TextInputLayout android:id="@+id/passLayout11" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" app:error="@{userdata.passwordError}" android:layout_below="@+id/userlayout11"> <EditText android:id="@+id/password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@+id/forgot_password" android:hint="Password" android:text="@={userdata.password}" android:inputType="textPassword" android:singleLine="true" android:textColorHint="#9F9F9F" /> </android.support.design.widget.TextInputLayout> <Button android:id="@+id/login" android:layout_width="280dp" android:layout_height="wrap_content" android:layout_below="@+id/passLayout11" android:layout_centerHorizontal="true" android:layout_marginTop="49dp" android:background="@color/colorAccent" android:text="Login" android:onClick="retrofitCall" android:textColor="#FFFFFF" /> </RelativeLayout> </FrameLayout> </RelativeLayout></layout>
Model- UserData.java
public class UserData extends BaseObservable { private String email; private String password; private Context context; public UserData(Context context) { this.context = context; } @Bindable public String getEmail() { if (email == null) { return ""; } return email; } public void setEmail(String email) { this.email = email; notifyPropertyChanged(BR.email); } @Bindable({"email"}) public String getEmailError() { if (getEmail().isEmpty()) { return "EMAIL IS EMPTY"; } return ""; } @Bindable public String getPassword() { if (password == null) { return ""; } return password; } public void setPassword(String password) { this.password = password; notifyPropertyChanged(BR.password); } @Bindable({"password"}) public String getPasswordError() { if (getPassword().isEmpty()) { return "PASSWORD IS EMPTY"; } else { return ""; } } } 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 public class UserData extends BaseObservable { private String email; private String password; private Context context; public UserData(Context context) { this.context = context; } @Bindable public String getEmail() { if (email == null) { return ""; } return email; } public void setEmail(String email) { this.email = email; notifyPropertyChanged(BR.email); } @Bindable({"email"}) public String getEmailError() { if (getEmail().isEmpty()) { return "EMAIL IS EMPTY"; } return ""; } @Bindable public String getPassword() { if (password == null) { return ""; } return password; } public void setPassword(String password) { this.password = password; notifyPropertyChanged(BR.password); } @Bindable({"password"}) public String getPasswordError() { if (getPassword().isEmpty()) { return "PASSWORD IS EMPTY"; } else { return ""; } }}
MainActivity.java
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActivityMainBinding binding = DataBindingUtil.setContentView( this, R.layout.activity_main); UserData obj = new UserData(this); obj.setEmail(""); obj.setPassword(""); binding.setUserdata(a); } } 12345678910111213 public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActivityMainBinding binding = DataBindingUtil.setContentView( this, R.layout.activity_main); UserData obj = new UserData(this); obj.setEmail(""); obj.setPassword(""); binding.setUserdata(a); }}
Thank you for reading it, maybe it will help you to understand the Two-way data binding.
Happy Coding, Stay Super and Stay Cool.
Your email address will not be published. Required fields are marked*
Name*
Email*
Save my name email and website in this browser for the next time I comment.
Be the first to comment.
We use cookies to personalize your experience. By continuing to visit this website you agree to our use of cookies. Learn more about privacy policy
Excellent work, fast, good quality and understood the brief perfectly! Quick responses developing the project and very good cooperation. I suggest to anyone.
Stathis Plakidas
USA
India
Global
Name
Email
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.