Demo that shows how to use RxJava with Android Data Binding ObservableFields. Without RxJava ;-)
Java
Switch branches/tags
Nothing to show
Clone or download
Pull request Compare This branch is 2 commits ahead of TangoAgency:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
app
gradle/wrapper
.gitignore
.travis.yml
LICENSE
README.md
build.gradle
gradle.properties
gradlew
gradlew.bat
settings.gradle

README.md

android-data-binding-rxjava

Android Arsenal android-data-binding-rxjava Build Status

Simple demo developed with love at Tango which demonstrates how to wrap Android DataBinding ObservableField into RxJava's Observable.

With this solution it is possible to register for ObservableField's value changes and use it with RxJava operators.

You can read Medium story which explains this concept - RxJava meets Android Data Binding.

Example code

public static class MainViewModel {

        public ObservableField<String> firstName = new ObservableField<>();
        public ObservableField<String> lastName = new ObservableField<>();
        public ObservableField<String> helloText = new ObservableField<>();
        public ObservableBoolean helloButtonEnabled = new ObservableBoolean(false);

        public MainViewModel() {

            Observable.combineLatest(toObservable(firstName), toObservable(lastName), (firstName, lastName) -> StringUtils.isNotNullOrEmpty(firstName) && StringUtils.isNotNullOrEmpty(lastName))
                    .subscribe(result -> {
                        helloButtonEnabled.set(result);
                        if (!result) {
                            helloText.set(StringUtils.EMPTY);
                        }
                    }, Throwable::printStackTrace);
        }

        public void buttonClicked() {
            helloText.set(String.format("Hello %s %s !", firstName.get(), lastName.get()));
        }
}

rxjava-databinding

How it works

You can find toObservable method implementation in RxUtils.java class.

It uses ObservableField's OnPropertyChangedCallback and expose property change events to the "RxWorld".