Xamarin.Android vs traditional Android
I have to be honest. It has been a while since I did an Android app but today I tried to remember my little knowledge to compare traditional Android Development and Xamarin.Android. To create this post I used Android Studio 1.1.0 y Xamarin Studio 5.7.2.
Even though the differences are very subtle, and the greatest one of everyone lies in the code, they do exist. I’ll start with the project structure:
- Activities (and other UI related code stuff). As you might know in Java exist the concept of
Packages, and it is strongly asociated to the underlying folder structure. In Android Studio the class
MainActivity.javais in the package
org.fferegrino.android.xevenshteinwhereas in Xamarin Studio the class `MainActivity.cs` is directly in the Solution's root.
- Application logic following the concept of Solution, when using C# we could easily place our application logic in another project, meanwhile, using Java the alternative is to create another package
- Manifest the manifest is the same, except in when declaring the entrey point to our application, in traditional Android it is done in this file, in Xamarin.Android it is declared elsewhere.
- Resources folder as the Manifest, the resources folder is the same with some minor changes in the naming conventions (uppercasing, full file names...), and as we will see later, the files itself are the same.
Layout files are the same in both approaches, in fact, I copied the content of
Main.axml (Xamarin) to
layout_main.xmland the result was the same. This worked too for string resources named
In Xamarin.Android our classes related to the UI have to derive from Activity as in traditional Android, here is the class declaration:
In the C# version you can see the
Activity decorator, which tells the name of the activity (
Label), wether it is the app launcher(
MainLauncher) and the icon (
Icon), these stuff goes in the Android Manifest in the traditional approach.
For UI elements such as Buttons, TextViews and EditTexts are equally named and declared:
By convention, in C# methods must start with an uppercase letter, and the override indicator is not an annotation but a keyword, this is the
Another interesting thing is the automatically generated
Resource class, when using Xamarin this class is named
Resoruce, and we can use it to find views:
As you can see, the diamond syntax provides a way to return the view without casting the view ourselves.
I left the best to the end. There are several ways to implement an event handler but C# has one of the coolest (using a Lambda expression):
There are no significant differences between traditional Android development and Xamarin.Android, meaning that if you have previous knowledge you can apply it without concerns but instead of writing Java you will be writing awesome C# code that can potentially be shared along any other platform.