How to use Observable class in Android DataBinding

In this android programming source code example, we are going to use Observable class in Android DataBinding.

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 use Observable class in Android DataBinding.

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="user"
            type="com.bluapp.androidview2.firebaseMlKitAndDataBinding.FirebaseMlKitAndDataBindingActivity12.ClassObservable"/>
    </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="@{user.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="@={user.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="@{user.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="@={user.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 androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.Bindable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.Observable;
import androidx.databinding.PropertyChangeRegistry;

import android.content.Context;
import android.os.Bundle;

import com.bluapp.androidview2.BR;
import com.bluapp.androidview2.databinding.ActivityFirebaseMlKitAndDataBinding12Binding;

public class FirebaseMlKitAndDataBindingActivity12 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityFirebaseMlKitAndDataBinding12Binding binding = DataBindingUtil.setContentView(this, R.layout.activity_firebase_ml_kit_and_data_binding12);
        ClassObservable userdata = new ClassObservable(this);
        userdata.setEmail("");
        userdata.setPassword("");
        binding.setUser(userdata);
    }

    public static class ClassObservable implements Observable {
        private String email;
        private String password;
        private Context context;
        private PropertyChangeRegistry registry = new PropertyChangeRegistry();

        public ClassObservable(Context context) {
            this.context = context;
        }

        @Bindable
        public String getEmail() {
            if (email == null) {
                return "";
            }
            return email;
        }

        public void setEmail(String email) {
            this.email = email;
            registry.notifyChange(this, 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;
            registry.notifyChange(this, BR.password);
        }

        @Bindable({"password"})
        public String getPasswordError() {
            if (getPassword().isEmpty()) {
                return "Password field is required";
            } else {
                return "";
            }
        }

        @Override
        public void addOnPropertyChangedCallback(OnPropertyChangedCallback callback) {
            registry.add(callback);
        }

        @Override
        public void removeOnPropertyChangedCallback(OnPropertyChangedCallback callback) {
            registry.remove(callback);
        }
    }

}

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