Binding an Android Library

Sample code for this post can be found at https://github.com/petermajor/SearchViewLayout

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 »

Await and the UI Thread

C# is a great programming language and it continues to evolve at a good pace even 15 years after it’s introduction.

One of the best features to ever be added to C# / .NET was async / await.

Early writers of asynchronous code used IAsyncResult and after that came the Task Parallel Library. But the TPL was still relatively difficult to use so most programmers only wrote asynchronous code when they really had to.

And then along came async / await which made our lives so much easier. Everyone and their dog can write asynchronous code. Hidden in that simplicity, however, is a lot of complexity and a few assumptions.

Today, we’re going to be talking about one of the biggest gotchas… It’s a suprising detail that a lot of experienced .NET developers don’t know about. It’s about await and the synchronization context.

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 http://developer.xamarin.com/samples.

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 »

Native cross-platform mobile apps with C# and Xamarin.Forms

And now for something completely different!

For the last 9 months I’ve been working on a mobile application built with Xamarin.

The first version of the app was built for Android using MvvmCross.

When I joined the project, we were just about to start the iOS version of the application and I put forward a proposal to switch out native layouts and MvvmCross and go with Xamarin.Forms instead.

Since then, I’ve learned a lot about mobile app development and Xamarin in particular. Using Xamarin.Forms in a production application has been fascinating, especially dealing with the ups and downs of building a cross-platform application.

Last month I had the pleasure of speaking at DDD South West 6 on building cross-platform apps with C# and Xamarin.Forms.

Here is a recording of the session and also the companion slides: