From JavaFX properties to reactive observables in Android programming

Previous year I was programming under JavaFX. I found, that they have introduced nice extension over old-fashion Java Bean convention.

In short

Old-fashion Java Bean convention expects, that each property is represented by two methods: so-called “getter” and, optionally, a “setter”. So, if you wish your object have property called message of type String, you define

public String getMessage() {

public void setMessage( String newValue ) {

Convention didn’t bother with how and where you will store this propery value. Nor property exist in language. It is some sort of “emergent” or “imaginary” thing. Other propgramming languages like C# have explicit property concept, but Java does not. In Java it’s only convention. Note Camel naming, and “get” and “set” prefixes.

However, this convention is used in many libraries and frameworks. For example, in Spring you can set property just by it’s name from configuration XML file:

<bean id="helloWorld" class="com.tutorialspoint.HelloWorld">
       <property name="message" value="Hello World!"/>

Example is from TutorialsPoint.

What if you want property to be observable?

In standard Java you are proposed to add several methods like

addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {

void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) {

Again, Java does not bother how you will implement housekeeping. Although library provides helper class PropertyChangeSupport. With this class I was usually doing something like this:

public void setMessage( String newValue ) {
   String oldValue = getMessage();
   if( !Objects.equals( newValue, oldValue ) ) {
      ... // housekeeping for property
      pcs.fireProperyChange("message", oldValue, newValue);

Property change code is global for a class, i.e. the same set of listeners can be maintained for all observable properties of the class. Additionally tradeoff is that you need to have property names in string form somewhere.

What in JavaFX?

In JavaFX they have expended convention. Additionally to setter and getter programmer is to provide third method:

public StringProperty messageProperty() {

Note, that property name is now goes first in method name, starts with small letter and appended with “Property” postfix. This is property accessor. It returns proprty itself, i.e. now properties “materialized”!

In more detail you can read it in Oracle’s article “Using JavaFX Properties and Binding”.

StringProperty is a member of big family of classes which you can extend and re-implement. Old-fashion getters and setters now may be rewritten as follows:

public String getMessage() {
   return messageProperty().getValue();

public void setMessage( String newValue ) {
   messageProperty().setValue( newValue );

The sugar is that all JavaFX properties are “bindable” by nature. “Bindable” means not only that you can listen property change as with conventional bean properties, but you can also wire one property to another via complex transformations chain and this wiring will hold all the time application runs.

For example, you can bind property of you messager like this:

   Bindings.when( mood.happyProperty() ).
      .then( "Hello World!" )
      .otherwise( "Goodbye Cruel World!" )

Here we have bound property “helloWorld.message” with property “mood.happy” via conditional expression. Whenever “mood.happy” will change, “helloWorld.message” will also change appropriatedly. The chain of listeners and updaters is created by JavaFX automatically. This is called “High-Level Binding API” and covered in article, referenced above.

Small runnable binding example is here on Java2S.


Unfortunately JavaFX propery model has some imperfections and didn’t get wide popularity. Particularly, there are no JavaFX properies on Android. If you wish to bind controls, states and so on, you are to code it different way.

Now, I am writing my first Android application and was thinking about, what library to use instead of JavaFX properties? I found that so-called reactive programming is actively developing and that there are libraries ReactiveX library for many languages, including Java.

Unfortunately, it is not 1:1 corresponding with JavaFX properies and making accents on different things like multithreading.

First problem I met, is that reactive observables are apparently not made to be object members at all. Of course, I am trying to resemble what I was learned in JavaFX, for example, if I want that my object contain observable property, I am creating a method

public Observable<Boolean> connectedObservable() {

As you see, I use postfix “Observable” instead of “Property” here. Don’t know if this has any sense.

Second thing is how to convert Android style notifications to observables. Android uses even older approach, when programmer is provided by abstract class or interface with abstract handler methods, which he need to implement.

For example, ServiceConnection interface. When you implement it, you are to override methods like “onServiceConnected”. What if I want to conver this call into reactive observable?

For now I came to the following solution with the use of BehaviorSubject:

private final BehaviorSubject<Boolean> connectedSubject = BehaviorSubject.create();

public Observable<Boolean> connectedObservable() {
    return connectedSubject.asObservable();


public void onServiceConnected(ComponentName name, IBinder service) {

Note, that I am making just very first steps in Android and this solution can be absolutely irrelevant with what is really required…

Turning ImageButton to toggled one

Was trying to make ImageButton s “toglable”. Recipes are everywhere and I made such program:

It shows “pressed” button by

protected void onCreate(Bundle savedInstanceState) {



And looks the following way:


Unfortunately, once I press “Home” button on the device, button turns unpressed. This happens even before application actually minimizes.

The solution for this appeared simple: I was to simulated toggle not with “pressed”, but with “selected” attribute.