Binding an Android Library

Sample code for this post can be found at

A year ago I wrote a post where I demonstrated embedding Java code in a Xamarin application. While the concept was pretty cool it was a contrived example.

I recently came across a Android Library written in Java that I considered using in my application. So here is an example of using a real-world Java library in a Xamarin Android application.


I wanted to have a search box in a toolbar. When a user taps on the search box it would expand and allow the user to type in the box.

This behavior can be seen in Google Maps and the Android Phone application. Fortunately for me, a very talented developer has already created an Android library to implement this behavior:


Read more »

MvvmCross and the Navigation Drawer

In my previous post, I talked about implementing an auto return toolbar using AppCompat and MvvmCross.

I indicated that it’s really important for MvvmCross to support toolbars in the layout and I went on to show some problems I’ve encountered with MvvmCross and toolbars in the layout in previous versions of the framework.

Today I’m going to show you another reason why you’d want to include a toolbar in the layout of an activity rather than letting the AppCompat theme do it for your automatically.

And this time it’s not to implement a feature as niche as an auto return toolbar. It’s to implement a feature that is becoming the defacto navigation standard for Material Design applications - the Navigation Drawer (a.k.a the hamburger menu).

Gmail Navigation Drawer

First, I’m going to implement a navigation drawer with standard Xamarin. After I get that working, I’ll add MvvmCross and implement each individual “page” as a view model.

So let’s go…

Read more »

MvvmCross and AppCompat Toolbar

One of the very cool things about Android programming with Xamarin is that anything that can be implemented for Android with Java can also be implemented with C#.

For example, say the designer on your project designs an activity with a quick return toolbar… Since this is possible to do on any modern version of Android with the Android AppCompat Library in Java, it should be just as easy to do in a Xamarin application with C#.

Unless you are using a framework on top of Xamarin Android. Then all bets are off.

This post shows the problems I encountered with implementing a quick return toolbar with Xamarin Android and the latest stable version (until two weeks ago) of MvvmCross - v3.5.1.

Read more »

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/

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>
    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…


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:

Read more »

Using Android Java libraries with Xamarin (pt. 1)

This is part 1 of a 2 part series: part 2

In this part, I will show you a cool feature of Xamarin.Android that you probably don’t know about: the ability to embed .java files directly in your C# project and then execute that Java code.

Java? Why?

Have you ever Google’d for how to do something in Android and found some Java code?

What are the options for using that Java code in your Xamarin app?

If it’s a large library, like an SDK, then you’ll want to get the .jar and create an Android Binding Library. That’s the topic of Part 2 in this series and we won’t talk about that further today.

If it’s a small bit of code, then your options are: 1. Convert the Java code to C# manually, 2. Include the Java code in your app and call it (the topic of this post)


I first learned about this feature by stumbling across a Xamarin sample.

If you’re not familiar with the Xamarin samples library, I recommend that you have a look through

Xamarin have a comprehensive library of samples for all areas of the platform. They’re really good at keeping them up to date as new versions of Xamarin are released.

It’s a great way to discover features that might come in handy one day.

Read more »