One of my recent projects had an interesting requirement. It was an Android application and it needed to be aware of any businesses that were near the user’s current location. If you have ever used Facebook’s or FourSquare’s “Check-In” feature, it’s very similar. The user had to tap a button, and bring up a list of all business that were near the user. The user would then choose a business (location), and then display the business’s Name, Address, City, State, Latitude, Longitude, etc.
When researching this functionality, I stumbled along the Google Places API. Upon examining the Google Places API, I noticed the PlacePicker widget. This is an out of the box widget that you can display programmatically and it will give you a beautiful Location Picker user interface similar to the Google Maps app for Android.
This meet my requirement, and then some. Not only did it give me a list of businesses that were near my user’s location, it also gave me a material design user interface that my users could interact with. That was an added bonus since I didn’t need to create a UI for this. I could just use the out of the box control and be done with it. To top it off, using the PlacePicker was easy! And on this post, I’m going to show you how to add it to your Xamarin.Android applications.
First Things First
The first thing you need to do before you start writing code is to acquire a Google Maps v2 API Key. If you already have one for your application, then great! Use that one! If not, you’ll have to acquire one. They are free, and there are a number of steps you have to perform in order to get one. Luckily, Xamarin has some detailed documentation on how to get one. So I recommend that you read that.
Next you want to download the latest Android SDK Tools and Google Play Services SDK from the Android Device Manager. I’m not quite sure what minimum versions you’ll need. But if you have a relatively recent version, then it should work. If not, just download the latest versions from the Android SDK Manager. If you need details on how to do that, I discuss this in the video below.
High Level Step By Step
I go into great detail in the video below, but as a reference here is what we’re going to do:
- Acquire a Google Maps v2 API Key.
- Download the latest, or have a relatively later version, of the Android SDK Tools and Google Play Services API.
- Create an instance of PlacePicker.IntentBuilder.
- Use StartActivityFromResult to launch an intent from your Place Picker Intent Builder.
- Override OnActivityResult so that the PlacePicker control can return data back to us.
- Get the Place that the user picked from the PlacePicker.
- Populate our UI from the Place object that the user chose.
That’s pretty much it at a high level. You can download the completed source code on my GitHub page here. Of course, a video is worth a million words! Watch the video and you can see the step by step details how to call the built-in PlacePicker control.
Feel free to leave a comment and let me know what you think!
I learned something new last week that I felt I probably should have learned a long time ago. As I know from my previous experience in Android development, ListViews only scroll vertically. In the past, there was no out of the box support for horizontal ListViews in Android. Developers had to create them manually or use 3rd party libraries hosted on GitHub. If you examined a 3rd parties source code, you would see implementing a horizontal ListView is not an easy task. With the release of Android 4.4 KitKat (API Level 19), a new control was added to the Android SDK. This new control is called a RecyclerView, and is available via the Android Support Library v7.
Luckily for us Xamarin Developers, we have out of the box binding to this library, so we can use this just like native Java developers. In this post I’m going to show you how to implement a vertical ListView (just like a standard ListView), as well as a Horizontal ListView. But we are not going to use the old legacy ListView control or a 3rd party library. We are going to use the now out of the box RecyclerView to implement both. The good news is that the since this is available in the Android Support Library v7, this control can run on devices from the current version of Android, all the way down to API Level 7 (Android 2.1 Eclair). So this control remains backwards compatible with almost any device out there. So let’s get coding!
In my first publish post I gave a very high level overview of what Xamarin is and what it isn’t. I then followed up with a post describing the nuts & bolts of Xamarin.iOS. In this article, I’ll be describing the nuts & bolts of Xamarin.Android. And as I started in my previous two posts, I think it is essential to understand what Xamarin.Android is, how it is compiled, and how it executes on your Android device. Though it is easy to jump right in and start coding, I believe it is essential to understand Xamarin.Android fundamentals, as well as the Android operating system fundamentals. In the long run, this will be beneficial when you have to start debugging your apps. It will also help you troubleshoot issues an understand what is happening between the Android operating system, Xamarin.Android, and your own code.
In the earlier days of Xamarin.iOS development (up to iOS 7), one of the more useful recipes from the Xamarin Documentation was the ActionSheet Date Picker. Adding a DatePicker as a subview of a
UIActionSheet was a very convenient way to display a DatePicker to the user. The Action Sheet would behave as it usually would, and you could modify it to slide from the bottom and only take up the bottom portion of the screen. After the user selects the date and taps on “Done”, the ActionSheet would then slide back down. Because of the behavior of the ActionSheet, this was a great way to provide a Modal Date Picker. However, this recipe no longer works on iOS 8 due to “misuse” of the
UIActionSheet. I created an alternative, with full source code and sample app that replaces the ActionSheet Date Picker that is free to use.
In my previous post I gave a very high level overview of what Xamarin is and what Xamarin is not. Though my post was more conversational and explained by example, I left out the technical details. In this post, I’m going to focus on the nuts & bolts of Xamarin.iOS. But before we start off coding a sample application, it is (in my opinion) essential to understand what Xamarin.iOS is, how it is compiled, and how it executes on your iOS device. Though it is easy to jump right in and start coding, I believe it is essential to understand the Xamarin.iOS fundamentals, as well as the iOS operating system fundamentals. In the long run, this will be beneficial when you have to start debugging your apps. It will also help you troubleshoot issues and understand what is happening between the iOS operating system, Xamarin.iOS, and your own code.
As of this writing, I have been actively developing for iOS and Android with Xamarin for almost 2 years. In that short time, the platform has grown rapidly and has matured quite a bit. I’ll be the first to admit that there still is some work to be done. But I’m confidant that by this time next year, the platform will have matured even better. It certainly has since I first started.
As a software engineering consultant for a firm, my colleagues often ask me what my current engagement is and what type of applications am I developing. When I mention that I’m developing iOS and Android apps using Xamarin, the majority have them have heard the term, and read about what it does. My current employer is a Microsoft Partner and one of 34 Microsoft National Systems Integrators. So naturally, the majority of my colleagues are .NET Developers. And some of the best I’ve worked with I might add.
But regardless, during our conversations about technology, specifically mobile development using Xamarin, I find that there are a lot of common misconceptions on what Xamarin is and what Xamarin isn’t. There are a good number of tools that help you write cross platform mobile apps that share a common architecture. However, I think that Xamarin is unique enough on their approach and philosophy on cross platform mobile development that they often unfairly get associated with other tools. So the purpose of this article is to explain what Xamarin is, and what Xamarin is not.