How to create two way databinding in Android

In this android programming source code example, we are going to create two way databinding in Android.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to create two way databinding in Android.

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"
    xmlns:tools="http://schemas.android.com/tools">
    <data>
        <variable
            name="userdata"          type="com.bluapp.androidview2.firebaseMlKitAndDataBinding.FirebaseMlKitAndDataBindingActivity18.DataBindingUserData"/>
    </data>
<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="30dp">

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/emailLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:error="@{userdata.emailError}"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintEnd_toEndOf="parent">

                <EditText
                    android:id="@+id/email"
                    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" />
            </com.google.android.material.textfield.TextInputLayout>

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/passwordLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:error="@{userdata.passwordError}"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toBottomOf="@id/emailLayout">

                <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" />
            </com.google.android.material.textfield.TextInputLayout>

            <Button
                android:id="@+id/signin"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="49dp"
                android:background="@color/colorAccent"
                android:text="Signin"
                android:textColor="#FFFFFF"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toBottomOf="@id/passwordLayout"/>

</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

MainActivity.java

import android.content.Context;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;
import androidx.databinding.DataBindingUtil;
import com.bluapp.androidview2.BR;
import com.bluapp.androidview2.R;
import com.bluapp.androidview2.databinding.ActivityFirebaseMlKitAndDataBinding18Binding;

public class FirebaseMlKitAndDataBindingActivity18 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityFirebaseMlKitAndDataBinding18Binding binding = DataBindingUtil.setContentView(this, R.layout.activity_firebase_ml_kit_and_data_binding18);
        DataBindingUserData userdata = new DataBindingUserData(this);
        userdata.setEmail("");
        userdata.setPassword("");
        binding.setUserdata(userdata);
    }


    public static class DataBindingUserData extends BaseObservable {
        private String email;
        private String password;
        private Context context;

        public DataBindingUserData(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 field is required";
            }
            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 field is required";
            } else {
                return "";
            }
        }
    }
}

app/build.gradle

dataBinding {
        enabled = true
    }

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

Add a Comment