MobileAndroidHow To Develop Mobile Applications in .NET with Xamarin

How To Develop Mobile Applications in .NET with Xamarin

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Mobile application development is now quite popular these days; there are plenty of frameworks available. Xamarin is a very popular framework used to develop mobile applications. In this article, we will explore the Xamarin framework for developing Android applications in .NET. This is the first part of a two-part series of articles on Xamarin. In this first part of the series of articles on Xamarin, we will learn what Xamarin is all about and how we can set up our system to be ready for developing Android applications using Xamarin for Visual Studio.

The first thing you need to do is download and install Xamarin for Visual Studio so that you can get started developing iOS and Android applications using Xamarin and C# in the Visual Studio IDE.

Getting Started

The minimum requirements for getting started with Xamarin for Visual Studio are given below:

  • Windows 7 or higher
  • Visual Studio 2010 Professional or higher
  • Xamarin’s plug-in for Visual Studio

You would also need to install Xamarin from the Xamarin website after signing up for a new account. Here’s the link: http://www.xamarin.com/.

You can also download the 30-day free trial of the Business Edition of Xamarin from this link: http://xamarin.com/download.

You should have the following installed in your system to get started using Xamarin for Windows in Visual Studio:

Also, you should take a look at this link to understand how to accelerate your Android emulator by using the Xamarin Android Player: http://developer.xamarin.com/guides/android/getting_started/installation/android-player/.

It should be noted that Visual Studio Express Edition will not work because it won’t support the plug-ins. Also, note that you should have a Xamarin account if you want to develop applications using Visual Studio; you would otherwise see a popup prompting you to create one and then use Xamarin in your Visual Studio IDE. Once all is set up, you should check the Android settings in your Visual Studio IDE by clicking Tools -> Options -> Xamarin -> Android Settings.

Your Android setting should point to the path where Android SDK is located in your system.

Working with Xamarin

Note that all Android applications basically start with an Activity; this typically would have an AXML layout. Note that the AXML format is actually an XML format used to describe the user interface on Android. Here is what the Main.axml file looks like.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns_android="http://schemas.android.com/
      apk/res/android"
   android_orientation="vertical"
   android_layout_width="fill_parent"
   android_layout_height="fill_parent">
   <include
      layout="@layout/progress_with_text"
      android_layout_width="wrap_content"
      android_layout_height="wrap_content" />
</LinearLayout>

You can use the Visual Studio IDE to develop your Android application and deploy it to an emulator that is configured to match your target Android machine.

To start developing an android application using Visual Studio IDE, follow these steps:

  1. Click File -> New Project.
  2. Select the template you need to use.
  3. You will see many templates there; select Android Application.
  4. Provide a file name for the project and click OK.

A new Android project will be created in your Visual Studio IDE. When you create a new Android project using Xamarin, you basically would find the following solution folders:

  • Assets: This would contain the arbitrary files, such as text, xml, fonts, music, and video the application would need.
  • Properties: This would contain the assembly metadata.
  • Resources: This would contain the application resources; in other words, strings, images, and so forth. It can also contain the declarative definitions for the user interface of your Android application.

You can place any assets that you would want to be deployed, along with your application, in the Assets folder. You then would be able to access these assets by using the AssetManager, as shown in the following code snippet:

public class LoadDefaultAsset : Activity
{
   protected override void OnCreate (Bundle bundle)
   {
      base.OnCreate (bundle);
      InputStream input = Assets.Open
         ("sample_asset.txt");
   }
}

An activity should derive from the Activity class and you can mention the label, the main launcher, and icon using the Activity attribute on top of your activity class. Here’s how an Activity class would look.

[Activity(Label = "Sample Android Activity")]
public class SampleActivity : Activity
{
   protected override void OnCreate(Bundle bundle)
   {
      base.OnCreate(bundle);
      SetContentView(Resource.Layout.Main);
      Button button = FindViewById<Button>
         (Resource.Id.MyButton);
      button.Click += delegate { button.Text =
         "Hi! Button Clicked..."; };
   }
}

A sample AXML file would look like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
   xmlns_android="http://schemas.android.com
      /apk/res/android"
   android_orientation="vertical"
   android_layout_width="fill_parent"
   android_layout_height="fill_parent"
   android_background="#0066CC">
</LinearLayout>

The following is the complete code for a simple Android application. Note how the SimpleAndroidActivity class extends the Activity class and overrides the OnCreate(0 method.

using System;
using Android.App;
using Android.OS;
namespace Internet.com.Android
{
   [Activity (Label = "My first Android Demo",
      MainLauncher = true)]
   public class SimpleAndroidActivity : Activity
   {
      protected override void OnCreate
         (Bundle savedInstanceState)
      {
         base.OnCreate (savedInstanceState);
         SetContentView (Resource.Layout.main);
       }
   }
}

Suggested Readings

Summary

In this first part of the series of two articles on Xamarin, we have had a basic idea of Xamarin, its benefits, and how we can get started using Xamarin for Visual Studio. Happy reading!

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories