Android App With Xamarin on Mac

I think I don’t like to be limited and thus I keep exploring and learning new technologies, however I made a conscious choice in college not to do Java (for whatever reason) and that kept me off Android for so long. But then, there came Xamarin! A cross platform mobile app development software.

Installation

Xamarin allows you to write code in C# and then build it for Android, iOS or Windows Phone. Visual Studio has it integrated and now we have a Visual Studio for Mac. I grabbed a copy online and installed it to go exploring…

vs_xamarin

New Project

Launch Visual Studio and pick an Android App project

screen-shot-2017-02-06-at-10-43-02-pm

Give your app a name and an organisation identifier, together they form the package name on Android Play Store. Complete the Wizard and you have a basic template ready!

Screen Shot 2017-02-06 at 10.46.39 PM.png

IDE

Visual Studio IDE has a familiar interface. The solution will have one project – TipCalculator in this case.

In Android, Resources > layout has Main.axml. “axml” files are created by Xamarin and are equivalent to xml layout files in native android apps. Layout means the UI.

screen-shot-2017-02-06-at-10-51-36-pm

The code behind file for this layout is MainActivity.cs. An Android app can be described as a set of activities, where each activity has a UI and some code-behind, and some extra files like images, data files, etc.

For example, Activity in this case is to calculate Tip, we will design a UI for the user to input bill amount and calculate tip and we will write code in the code behind to make it work. There can be multiple activities in a complex scenario and navigation from one UI to another while you interact with the app through widgets/controls.

The App

We need to create a UI first. Open Main.axml in designer/source editor and add an editText to receive input, a calculate button and a textView to display calculated Tip value.

screen-shot-2017-02-06-at-11-06-28-pmscreen-shot-2017-02-06-at-11-07-56-pm

We added an ‘id’ to every control in the UI. This will be accessed in code behind file to make it work. A corresponding ID is generated in Resource.designer.cs

screen-shot-2017-02-06-at-11-12-24-pm

Now some code in code behind file – MainActivity.cs

 using Android.App;
 using Android.Widget;
 using Android.OS;
 using System;
 
 namespace TipCalculator
 {
     [Activity(Label = "Tip Calculator", MainLauncher = true, Icon = "@mipmap/icon")]
     public class MainActivity : Activity
     {
         EditText inputBill;
         Button cmdCalculate;
         TextView outputTip;
 
         protected override void OnCreate(Bundle savedInstanceState)
         {
             base.OnCreate(savedInstanceState);
 
             // Set our view from the "main" layout resource
             SetContentView(Resource.Layout.Main);
 
             inputBill = FindViewById<EditText>(Resource.Id.edittext1);
             cmdCalculate = FindViewById<Button>(Resource.Id.button1);
             outputTip = FindViewById<TextView>(Resource.Id.textView3);
 
             cmdCalculate.Click += onCalculateClick;
         }
 
         void onCalculateClick(object sender, EventArgs e)
         {
             var bill = double.Parse(inputBill.Text);
             outputTip.Text = (bill * 0.05).ToString();
         }
     }
 } 

 

In this code, Label=”Tip Calculator” set the application title. MainLauncher=true is set to make this Activity start on process launch.

Next, OnCreate() takes an argument of type Bundle. This is passed every time the activity window is destroyed and recreated, and that happens very often including the time we rotate. The previous state is saved in a bundle and calling the constructor with the bundle helps restore the cached state.

To instantiate a layout, we find the layout using Resource.Layout.Main and set it the current view. This is done by the Id that was created in Resource.designer.cs and maps to the right axml file. This builds the UI and shows it for the current activity.

We then search for Id for the controls of our interest on the UI. In our axml, we declared Id as

android:id=@+id/textView3

This generates an integer Id for the name – textView3

// aapt resource value: 0x7f0c0055
public const int textView3 = 2131492949;

Set an event to button click and add logic to it.

cmdCalculate.Click += onCalculateClick;

Screen Shot 2017-02-06 at 11.25.35 PM.png

 

Wow! First Android App, \with basic knowledge of application development on Windows (.NET) this was quick!

Tip

Application can be deployed to an emulator and tested. If you get deployment error for x86

Deploying package to 'emulator-5554'

The package does not support the device architecture (x86). You can change the supported architectures in the Android Build section of the Project Options.

Deployment failed. Architecture not supported.

Right click on Project > Options > Build > Android Build > Advanced tab and select x86.

Screen Shot 2017-02-06 at 11.29.28 PM.png

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s