Creating Android Apps with Xamarin in Visual Studio
Xamarin platform helps .Net developers to easily build applications for Android, iOS, Mac and Windows in C#. Let’s take a look at how to create our first Android demo with Xamarin in Visual Studio 2013.
Xamarin Installation and Configuration
Download Xamarin:
Connect to the Internet, and run the Xamarin installer. Make sure you have already configured Java environment correctly, and then all needed packages will be automatically downloaded:
By default, the Android SDK is installed at C:\Program Files (x86)\Android\android-sdk. Use SDK Manager.exe to install corresponding system images, and run AVD Manager.exe to create an Android virtual device (AVD):
First Android App in C#
Launch Visual Studio 2013 to create an Android project:
Click Main.axml to see the layout:
Check the code inside MainActivity.cs. A button with click event:
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace HelloXamarin
{
\[Activity(Label = "HelloXamarin", MainLauncher = true, Icon = "@drawable/icon")\]
public class MainActivity : Activity
{
int count = 1;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
// Get our button from the layout resource,
// and attach an event to it
Button button = FindViewById<Button>(Resource.Id.MyButton);
button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); };
}
}
}
That’s all. Let’s try to build and run the app to see what will happen: An error alert: Deployment failed due to an error in FastDev assembly synchronization!
Don’t panic. To fix the issue, click project Properties -> Android Options, and uncheck Use Fast Development:
Rebuild the project. It’s successfully running now in the emulator: