Bindroid – A Binding Framework for Android

I’ll admit it — I was spoiled by my days on the Silverlight team, playing with XAML, .NET, and databinding.  When I started building Android versions of my apps, I went in with the expectation of a similarly modern, powerful UI framework, but quickly found myself frustrated by the complexity of creating UI that tracks values as they change in my application.

Unlike XAML-based UI frameworks, Android doesn’t really have a standard change notification mechanism, nor does it have databinding functionality.  For my apps, I found it useful to build a general-purpose framework that I could use to build really rich, responsive user experiences with minimal effort.  I’ve decided to open-source this framework as Bindroid, in the hopes that others might find it useful as they build their apps as well.

Bindroid makes binding your UI to your data really easy.  Just use a “Trackable” to implement your properties (in Java, I consider this to be a pair of methods on an object named get<Name> and set<Name>) like so:

public class FooModel {
  // An int property named "Bar"
  private TrackableInt bar = new TrackableInt(0);
  public int getBar() {
    return bar.get();
  }
  public void setBar(int value) {
    bar.set(value);
  }

  // A String property named "Baz"
  private TrackableField baz = new TrackableField();
  public String getBaz() {
    return baz.get();
  }
  public void setBaz(String value) {
    baz.set(value);
  }
}

Trackables are quite magical — they “infect” anything they touch with observability, so that even calculated properties can be observable:

public String getBarBaz() {
  int strLength = (getBaz() == null ? 0 : getBaz().length());
  if ((strLength + getBar()) % 2 == 0) {
    return "Baz's length + Bar was even.";
  }
  return String.format("Bar: %d, Baz: %s", getBar(), getBaz());
}

Once you’ve implemented your properties this way, binding to them is a one-liner in your Activity’s onCreate(). For example, the following line binds the Text property of a TextView named barBazTextView to the BarBaz property we just defined:

FooModel model = new FooModel();
UiBinder.bind(this, R.id.barBazTextView, "Text", model, "BarBaz", BindingMode.ONE_WAY);

As you manipulate the values of the model, barBazTextView‘s text will automatically update to reflect the new value of BarBaz. It’s that simple!

I’ve found that whether binding to individual values or collections of values, Bindroid dramatically simplifies the process, and makes developing Android user interfaces a much more pleasant experience.

I hope you’ll give it a try! You can learn more about (and get a copy of) Bindroid by visiting the GitHub repository. It’s all available under the MIT License, so have at it!

4 thoughts on “Bindroid – A Binding Framework for Android”

    1. This is sort of orthogonal to the notion of remote data. An app with remote data might, for example, set trackable fields on the client when the data becomes available, and the UI will automatically update with the latest data.

  1. Hi David,

    I’ve been investigating using your Bindorid framework and i think it is great – thanks for putting the time and effort into it!
    I come from a .Net background and like you found it hard to relate to the fact there wasn’t anything similar to the binding that you have with .net. This becomes especially problematic when trying to design complex UI’s with many inputs for business apps.

    I have managed to implement the binding for textviews within a list fragment and EditText boxes, as per your example, but i am struggling slightly with implementing it with other UI controls such as Spinners and Buttons. Do you have any examples of how to do this?

    For example, i have a spinner that has an ArrayAdapter containing 2 values. I want to bind the value of the spinner to a TrackableField in an object, thus updating the object when someone selects a different value from the spinner.

    Once again, thanks for investing the time and making it available!

    Simon

  2. Hi David,

    I am kind of new to Android Development as well, but doing a little bit of research I came across your framework and Andy Tsui one. You guys have done something ‘similar’ in a way… Probably Andy’s is a little bit more mature.

    I am a little bit of an advocate of good practices and uniformity and give you have some unique skills and have invested some time on your framework… have you considered joining forces with Andy’s and collaborate there? It would be incredibly useful to develop the full databinding kind of functionality for Android.

    Cheers!

Leave a Reply