How to use BaseObservable class in Android DataBinding

In this android programming source code example, we are going to use BaseObservable 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 BaseObservable 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.FirebaseMlKitAndDataBindingActivity11.ObservableBase"/>
    </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.BaseObservable;
import androidx.databinding.Bindable;
import androidx.databinding.DataBindingUtil;

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

import com.bluapp.androidview2.BR;
import com.bluapp.androidview2.R;
import com.bluapp.androidview2.databinding.ActivityFirebaseMlKitAndDataBinding11Binding;

public class FirebaseMlKitAndDataBindingActivity11 extends AppCompatActivity {

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

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

        public ObservableBase(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