I have recently been learning Cordova, and I wondered how it compared with writing applications in Xamarin. I conducted a poll on Twitter:
With a massive 80% of the votes saying Xamarin
is the best solution, I knew it would be well worth at least learning the basics of it.
So here is a review of Building Your First Xamarin.Android App From Start to Store by Gill Cleeren.
There are a few different Pluralsight courses on Xamarin but I chose this one because it was published recently and is a beginner friendly course.
Gill works as a .NET architect and is a Microsoft Regional Director. He’s given many sessions and webcasts on new as well as existing technologies, such as Silverlight, ASP.NET and WPF at conferences including TechEd, TechDays Belgium – Switzerland – Sweden, DevDays NL and NDC Oslo Norway.
Gill has also produced a sister course Building Your First Xamarin.iOS App from Start to Store.
The reason I chose the Android course is just because I currently have an Android rather than iOS phone.
The iOS course builds the same mobile app as the Android course, only the device operating system is different.
Gill explains what the course entails and what is expected for you to already know coming into this course.
This course is for people who:
- Already know some C#
- Have a basic familiarity with Android
- Want to leverage what they already know
For this course Gill uses the Xamarin Business Edition 30 day free trial. There is a free Xamarin starter edition.
You can download Xamarin here. Windows users can either code in Visual Studio or in Xamarin Studio, Mac users need to use Xamarin Studio.
Don’t skip past this module because Gill also discusses emulator options here:
Gill explains the requirements for the app we will be building, which is called
“Rays Hot Dogs”
Gill then demonstrates the finished mobile hot dog app that you will have developed by the end of this course.
Setting up Your App Solution
Overview of the Xamarin Platform
Gill describes the different languages used to build apps for each mobile operating system and the difficulties in reusing code before Xamarin was created.
Gill calls this the silo’d approach. The apps have the best performance due to native code, but are very expensive to implement and maintain.
He talks a bit about how Xamarin compares with Ionic and Cordova. (If you’re interested in learning Ionic, see Building Mobile Apps with the Ionic Framework and Angular JS).
Gill describes Cordova as the “lowest common denominator” – building a mobile website wrapped in an application package. He says this is much cheaper to build than using the silo’d approach but won’t always produce the best performing apps.
We learn that, as well as the code sharing benefits you get from Cordova, Xamarin apps use the native UIs, have access to all native features of the platform and studies have shown they have the same performance as apps built in the native languages.
Cordova based solutions are hybrid apps, whereas Xamarin apps are native apps. I found the Telerik article Native vs HTML5 – you’re asking the wrong question quite helpful in explaining these differences.
The major disadvantage that Xamarin has compared to Hybrid and mobile web solutions is it is not a shared UI platform: you must use XML files for Android, storyboards for iOS and XAML for Windows phone apps.
Gill explains Xamarin Forms allow a shared UI platform, but that is not covered in this course. Jesse Liberty has a course called Xamarin.Forms and Data – hopefully I can also review that for you later.
Gill also briefly describes the learning curve associated with Xamarin here, and the licence fee for business users.
This course focuses on the first of the three main elements of the xamarin family of technologies:
Gill explains that we will be using the Mono framework, and that we can use Visual Studio with the business edition of Xamarin, but must use Xamarin Studio if we are using the Starter Edition.
Since this course was recorded, Microsoft bought Xamarin, and you can now use Xamarin with Visual Studio Community edition for free. Compare Visual Studio.
I have written up a summary of the Xamarin Evolve keynote to bring you up to speed with many of these changes.
Visual Studio Professional 2015 update 2 has all of the Android project templates available as long as you include Xamarin in the installation options.
Also see the Visual Studio Blog announcement.
Demo: Xamarin in Visual Studio
Gill demonstrates how to create a stand alone project, and how to run it. Gill uses the Hyper V emulator. We see where the .apk file is created. This is the Android package which is deployed to the emulator.
Setting up the Solution
Gill explains that we want to reuse as much code as possible across different platforms. With Xamarin, not 100% of the code you write will be reusable, but just about all of your backend code can be reused, including:
- Data layer
- Data access layer
- Service access layer
- Business layer
Gill says on average 50-60% of your code can be reused, and explains a couple of options for achieving this:
Portable Class Libraries (PCLs) – these are described, including how to create them in Visual Studio or Xamarin Studio
Shared Projects – also described but Gill warns that this gets messy in big project and is not a big fan of these.
Demo: Setting up the Solution
We start off with a blank RaysHotDogs solution, add a PCL project, and in this project ad our HotDog model class.
Next we have a HotDogGroup and a HotDogRepository with a bunch of hardcoded data.
Finally we add a HotDogDataService.
Creating the app and first view
Android development is in many ways different from Windows development, so Gill explains the fundamental concepts here.
This is the longest clip in the course, and includes:
- Android SDK versions
- Project structure
- Activities – the beating heart for Android development
- The Default Activity
- Activity States: Running/Paused/Backgrounded/Stopped
- The most important Lifecycle methods for an activity
- Views (.axml files) and their location
- The Default View
- Sample View Code
- The LinearLayout, TextView, EditText and Button XML tags
- Linking the View and the Activity
- The magic: Resource.Designer.cs
- Accessing Controls from Code
- Supporting Many Resolutions
- Application Manifest file
Demo: Adding the Xamarin.Android project
Gill adds new Blank App (Android) project
Description of MainActivity.cs, including an
explanation of the Activity attribute
Description of Main.axml
Description of Resource.Designer.cs
Creating Our First View
A quick overview of the Designer Features in Visual Studio and Xamarin Studio
Demo: Taking a Look at the Designer
Gill shows the designer features in Visual Studio, adds a button using drag and drop, and shows multiple ways to resize it.
In this example we are working with device independent pixels, e.g. 116.0dp
We also see what our app would look like on an Android wear watch (not great!)
Layout and Regular Views
Gill explains the four most important layout views are:
We see examples of all of these.
Next, Gill describes the Base Views:
Demo: Creating the Detail View
We create the HotDog detail view and the HotDog detail activity in Visual Studio
Adding a Menu Page
Creating the Menu Page
Android frequently uses lists, e.g. in the Settings Screen, and in the Google Play store.
Gills explains the Building Blocks necessary to create a list: ListView, Rows and Adapters.
We pass data from our code to the Adapter, which is linked with the ListView.
We see a screenshot of the Fast Scrolling and hear a description of it.
Gill explains the relationship that the participating classes (BaseAdapter, Adapter, Custom Adapter and ArrayAdapter<T>) have with each other (starting 3:54)
And we see the code for a simple ListActivity, which uses ArrayAdapter<String>
We learn that we will usually need to build own own custom Adapter, and see some code showing how to do this.
Finally, Gill discusses Row Reuse (starting 8:44) and the graveyard!
Demo: Creating the Menu Page
Now Gill puts all of the theory that’s just been discussed into action, creating a HogDogMenuActivity, HotDogMenuView and HotDogListAdapter.
He enables the Fast Scrolling feature.
Changing the Row Style
Gill states that we can do better! He discusses some of the built-in row styles, such as TestListItem and ActivityListItem.
Demo: Using the Built-in Styles
In this demo, we update our HotDogListAdapter to make use of the ActivityListItem Template.
Each row is now composed of an image and a text label.
Creating a Custom Row Style
A custom row style is built from a .axml file, and some code which we see here.
Demo: Creating a Custom Row Style
Gill adds HogDogRowView.axml, updates the LinearLayout attributes, adds an ImageView, and then a RelativeLayout.
Navigating between Activities
Gill begins by describing the common navigation patterns in Android: Stack navigation, tab navigation and drawer navigation
Stack navigation is the most basic, and most commonly used type, e.g. it is used in the Android Settings screen.
Navigation is done using an Intent: a description of an action that needs to be performed. Gill explains this further, including explicit and implicit intents, and shows the code to create an Intent.
Next we learn about passing data between Activities (5:10). It’s not possible to pass an entire object from one activity to the other, but Gill explains what you can pass.
Gill Lists use of Camera, Contacts and Phone as typical examples of using Implicit Intents.
Demo: Navigating Between Activities
We begin with a look at MainMenu.axml and then move to MenuActivity.cs where Gill shows us the necessary private fields, FindViews method, and HandleEvents method.
HandleEvents calls OrderButton_Click which instantiates our Intent.
The same basic process applies to our AboutActivity as well, where we use an ActionCall Intent to make a Phone Call.
Making a phone call requires extra permissions from the user. In the Android Manifest we check the CALL_PHONE required permission.
We see that making a phone call cannot work when run on the emulator.
Gill also update the HotDogDetailActivity with OrderButton_Click functionality
Adding Tabs to the Menu Page
This begins with a description of the different ways we can work with tabs: TabHost, AppBar/ActionBar, and ViewPager.
In this course we work with tabs using the ActionBar.
Before we can hop into a demo, we must understand the concept of fragments: UI Modules which are a bit like user controls in .NET. Gill explains that they promote code reuse, support different resolutions, and have their own lifecycle.
Demo: Adding Tabs to the Menu Page
We begin by adding a BaseFragment.cs in a new folder. Then we add FavoriteHotDogFragment.cs, MeatLoversFragment.cs and VeggieLoversFragment.cs
Using Advanced Android Features
The main features of this module are taking pictures with the camera, integrating maps in our app, and talking to a REST service.
Taking Pictures with the Camera
We learn that there are different options for taking pictures: we could use a camera intent, or we could use the Xamarin.Mobile framework.
We see a code snippet for using the camera intent.
Demo: Taking Pictures with the Camera
We run through the additional code in MenuActivity.cs which implements the camera photo functionality.
Integrating Maps in Our App
As far the Camera functionality, there are a couple of options for using maps with our app. We could forward the user to the built-in maps application, which is the easy option.
Alternatively we can integrate the map inside our app. This requires the Google Play Services are installed on the device. We can also use the map to add overlays, e.g. a pushpin on a location of interest.
We see a code snippet for forwarding to the map app, using an ActionView intent.
Demo: Forwarding to the Map App
This is the easy option, requiring only a few lines of code, which Gill describes here.
In this demo, we focus on the marketplace of Brussels, the location for Rays Hot Dogs.
Using the Google Maps API
Although this option requires more work, it creates a better user experience. Gill explains that we can use the Google Maps API to display the map inside of our own app, and to interact with this map, such as changing locations, zoom levels, map type etc.
The first thing to do is to install the Google Play Services SDK. You can do this using the Android SDK Manager. (You can learn more about the Android SDK in the Android SDK Tools module of Larry Schiefer’s Exploring Android Studio).
Gill explains that Google Play Services is a commercial product, and not all users will have it installed on their mobile device.
You will also need to register for a Google Maps API key.
Demo: Using the Google Maps API
Gill adds a FrameLayout, and in RayMapActivity.cs add some private fields and methods FindViews, HandleEvents, CreateMapFragment and UpdateMapView.
The map that is displayed is of type MapFragment. In CreateMapFragment we instantiate GoogleMapOptions with our map type, zoom controls enabled and compass enabled.
UpdateMapView loads the map inside of our MapFragment using mapFragment.GetMapAsync and passing in a callback handler of type LocalMapReady. Gill explains the purpose of LocalMapReady and we look at some of the code from metadata.
UpdateMapView also uses a CameraUpdate instance and to find the location to move the Google Map camera to.
Gill explains the half a dozen additional permissions that you will need to add to your Android Manifest.
Talking to a REST Service
Mobile apps themselves should not have a direct connection to a database, instead they should use a service.
Xamarin supports the following services:
– WCF (with some limitations)
– ASMX (old)
Gill introduces REST services and some of their many advantages.
For more detailed information on REST, see the Pluralsight course REST Fundamentals.
We see sample response messages in XML and JSON.
Xamarin uses WebClient and you can find more details here:
Demo: Talking to a REST Service
At the end of this demo, we see that we are still getting all of our data, but we are now getting it from the server rather than just having it hardcoded on the client.
Preparing for Store Deployment
Adding Application Icons
No app should be deployed in the store with the default icon that Xamarin uses, but adding an application icon is not difficult.
Gill describes application resources, including the various drawable directories and image files.
Android determines the pixel density of the device and look in the appropriate drawable directory for the relevant image.
The “drawable” directory (without any dpi in the name) is the fallback for devices that do not use the other resolutions.
###Demo: Adding Application Icons
We add our custom images, appicon.png and smallicon.png, into the drawable folder.
For brevity, we don’t see it in this clip, but this is the right time to also add different resolution versions of these images into each of the other drawable folders, such as drawable-hdpi, drawable-ldpi etc.
Gill updates the Activity attributes in MenuActivity.cs, giving the Label “Ray’s Hot Dogs” and the Icon “@drawable/smallIcon”
Translating the App
We learn that in order to support multiple languages, we use Strings.xml inside Resources/values.
We see the markup for adding a string resource in Strings.xml, and the markup we add to our XML Layout file.
Next, we learn that we have a separate folder for each language, with the two character language code and the end. For example, values-nl
If you have a .NET background, you will find this very similar to how Localization works with Global Resources.
Demo: Translating the App
Now we implement these changes in Visual Studio, adding ApplicationName and orderButtonText strings in English.
Gill shows us what is created in Resource.Designer.cs
Next we copy the values directory, rename it to values-nl, and update the Strings.xml values to Dutch.
Packaging and Distributing the App
Gill explains the important steps:
– Disable debugging
– Adopt a versioning strategy
– Configure the linker
– Set your package properties
– Compile in release mode
– Create the archive
With regards to adopting a versioning strategy, Android uses Version number, and version name.
The Version number is an integer, and the version name is a string.
Gill shows the Linker properties available:
– Sdk Assemblies Only
– Sdk and User Assemblies
We learn that, when configuring the linker, we should be aware that the Mono framework is packaged with our application, and that we usually don’t need everything inside of this framework.
The linking removes the unused parts and dramatically reduces the application size.
Gill then also explains the three steps for signing the Android package:
1. Private key store
2. Sign your package
3. Zipalign the package
And finally we learn what our Distribution options are:
1. Google Play Store
2. Alternative Store (e.g. Amazon App Store)
3. Download link for your APK on your website
4. FTP, Email, etc.
Demo: Packaging and Distributing the App
Gill shows us how to update the AssemblyInfo file to disable debugging, and set Version number and name in the Android Manifest.
Next, we see how to configure the Linker, and publish our Android app, using an existing key store.
We see our APK and signed APK files in our Release folder, which are ready to be uploaded to Google Play.
I found this to be an excellent and thorough introduction to Xamarin.Android. There is a good chance that I will also be watching Gill’s Xamarin.iOS course soon. Another good bit of news is Gill is working on a more advanced follow up course, which will build a large app in Xamarin.