data binding

Android Data Binding (pt. 4)

This is part 4 of a 4 part series: part 1, part 2, part 3

In the last post we examined some of the internals of the Android Data Binding Library.

In particular, we discovered that the current implementation of data binding is “one-way”. That is to say that if we bind a string value in a view model to an EditText, a change in the EditText value does not propagate back to the view model.

This might be unexpected for developers who have used data binding in other frameworks, like WPF or AngularJS. These frameworks support “two-way” binding out of the box.

So we’ll need to implement our own EditText watcher to propagate changes from the view to the view model. What might that look like?

Watching the EditText

The easiest way to watch the EditText is to implement a watcher.

Let’s create a SimpleTextWatcher that only requires only one method to be overridden:

Read more »

Android Data Binding (pt. 3)

This is part 3 of a 4 part series: part 1, part 2, part 4

In previous posts we got an Android application up and running with the Data Binding Library with binding to an EditText. We also bound a Button click to a command on the view model.

However, there was a problem with the code that we left in step 3. Did you spot it?

The issue is… if you change the text in the username or password EditText and then click the Sign In button, the username and password properties on the view model will still have the original string values, not the changed values… Huh, why’s that?

Understanding Android Data Binding

Under the covers, Android data binding is really just code generated by an Android Studio plug-in that connects the view and the view model.

You can examine the generated code here: app/build/intermediates/classes/debug/com/petermajor/databinding/databinding/ActivityLoginBinding.java

Read more »

Android Data Binding (pt. 2)

This is part 2 of a 4 part series: part 1, part 3, part 4

In the previous post we got an Android application up and running with the Data Binding Library with some simple one-way binding.

In this post, we’re continuing the implementation of LoginActivity, attempting to build the view and view model using the MVVM pattern.

Binding the Button

Let’s revisit how we would bind a button click to a view model method with binding in Xamarin Forms :

<Button Text="{i18n:Translate LoginButton}" Command="{Binding DoLoginCommand}" />
public class LoginViewModel : BaseViewModel
{
  Command _doLoginCommand;
  public Command DoLoginCommand
  {
    get { return _doLoginCommand; }
  }

  public LoginViewModel()
  {
    _doLoginCommand = new Command(x=> Login());
  }

  async Task Login()
  {
    // do stuff
  }
}

Let’s consider another example, using HTML and AngularJS:

<button ng-click="login.onSubmit()">Sign in</button>
angular.module('app.login')
  .controller('LoginController',
    function() {
      this.onSubmit = function() {
        // do something
      };
    });

Read more »

Android Data Binding (pt. 1)

This is part 1 of a 4 part series: part 2, part 3, part 4

Google introducted Android Data Binding Library at Google I/O this year.

With my WPF background and having used Xamarin Forms for the last year, I’m intrigued to see how Android’s new data binding approach compares with other frameworks. If you are too, read on…

Setup

One of the things I love about Android development is that whatever issue or question you have, countless people have solved it before you. Google’ing any Android development question yields more search results than you can read.

That, however, is not the case for the data binding library. It’s a really new feature (still in RC as I write this post) and pretty much any sample you will find will be how to bind a simple property to a read-only label.

The goto article for getting started is a single page in the Android documentation: https://developer.android.com/tools/data-binding/guide.html.

Read more »