Short introduction about the technologies
Build native applications for iOS, Android and Windows from a single, shared C# codebase, customizing the UI of the app in XAML. Right now Xamarin is best used with Microsoft Visual Studio, now both on macOS and Windows.
Why would we use Xamarin Forms?
It’s simple: instead of creating 3 applications that are, fundamentally, the same (same logic, same gestures, same functionality and, in essence, with the same UI – User experience depending only on the targeted OS) in (at least) 3 different languages or with 3 different development environments, we can do this in a single IDE with only one programming language, C#, customizing the UI with XAML.
Why would we use Android Studio?
Our main target is the Android World. Starting from mobiles, to tablets, to wear and anything that runs Android. We desire that our application will run exactly as designed and we try to optimize it to its maximum on a specific set on devices. We keep it small but we do our best for those devices.
Talking about Fortune
For this blog we are going to discuss about FortuneApp, a simple yet powerful (for teaching purposes) mobile Application, targeted for Android, that uses a Fortune API (JSON parsing, but I did a manual parse) alongside with media playback and a web request. From now on, I will be presenting from 2 points of view, both regarding the Android Application:
- Developing this app in Visual Studio, using Xamarin Forms (I will refer to it, for simplicity as Xamarin).
- Developing this app in Android Studio (I will refer to it, for simplicity as Android).
I tried to make them look the same, witch was quite simple, but I kept the default settings on some views since I wanted to show that there are some User Experience differences from Default Xamarin and Default Android.
If you did not guess, the one on the left, with the black Status Bar, is developed in Xamarin and the one on the right, with the blue Status bar, in Android.
They are quite the same, aren’t they? It consists of a Linear Layout (Android) and a Stack Layout (Xamarin), which are equivalent, that contain Layouts for (in this order): Text, Image, Text, Text, Button.
For both, the magic happens when the user touches the “GET SOME FORTUNE!” button, then there happens a web request for a JSON, from the web API, in the Xamarin I create a Thread for the Text to be modified and in Android an Asynchronous Task, to give me the data. Then, in both I parse the data as I desire, Strings, not a JSON Parser. Async vs Thread.
Then the parsed text goes to the Text View. Also, when the button is pressed, there starts the media playback for a sound (“hmmmm”).
As you may have noticed the code is quite the same, I also tried to implement the as close as possible, but the truth is, it’s as easy to write in Java as it is in C#, at least for this example.
The major difference is the logic of each environment:
- As you should know, Xamarin uses a common logic for all of its components so most of the code is in the shared part, this aids us a lot because it’s faster to write 3 applications that share 90% of the code. For this example we needed the media playback feature, for witch i used the Dependency Service. For Android, we must write only one to have one. So we have write one and 3 small chunks and have 3 applications VS write one and have one application, you decide.
- The Android Manifest is present in both cases, but the one in The Android Studio is not so user friendly, Xamarin had it simplified.
- C# and XAML VS Java and XML, they seem the same for me, but everyone is more comfortable in his own Language(s).
- Activities, Classes and Layouts vs Classes (and XAMLs, but not always).
In the end (It doesn’t even matter!)
Sorry for the pun, but the conclusion it’s that: It doesn’t even matter. As you have seen, both have the same potential, similar implementations and identical results. What matters:
- What you want to develop: 1 Android application (Android Studio can make it run faster, usually) vs 3 Applications (Xamarin will write them faster).
- What you are comfortable with: Java/C# and XML/XAML.
- Targeted hardware: if you did not already know, Android Studio has for every single Android Device something already done (a hello word configured for the targeted device or devices), for Xamarin, you will have to do it yourself (you may need to even write drivers sometimes).
Thank you very much for reading this and I hope that this was helpful in aiding you chose what you will use for development, until next time!