Java – Use ObservableBoolean or Observable to combine two observable boolean values

Use ObservableBoolean or Observable to combine two observable boolean values… here is a solution to the problem.

Use ObservableBoolean or Observable to combine two observable boolean values

In JavaFX, you can combine two observable boolean values by doing the following:

BooleanProperty imagesDownloaded = new SimpleBooleanProperty(false);
BooleanProperty animationComplete = new SimpleBooleanProperty(false);

BooleanBinding isValid = imagesDownloaded.and(animationComplete);

How can I do the same using RxJava or Google’s Databinding API?
I also want to listen for changes in the value of the isValid variable.

Solution

You can use Observable.combineLatest(). , Observable.zip() , >Observable.merge(), and others operators depend on your goals.

I’ll add a short example to show what this looks like in RxJava:

<pre class=”lang-java prettyprint-override”>PublishSubject<Boolean> property1 = PublishSubject.create();
PublishSubject<Boolean> property2 = PublishSubject.create();

Observable.combineLatest(property1,
property2,
(propertyOneValue, propertyTwoValue) -> propertyOneValue && propertyTwoValue)
.subscribe(isValid -> doWork(isValid));

sometime later
property1.onNext(true);
property2.onNext(true);

Related Problems and Solutions