Building Android Applications using C#


Building Android Applications using C#

MonoDroid is a software product developed by Novell to build Android based mobile applications using C# and .NET. To install and work with MonoDroid, you need Visual Studio 2010. MonoDroid works as an add-on of Visual Studio 2010. Once installed successfully, the MonoDroid project templates are available in Visual Studio 2010. MonoDroid does not work with Visual Studio Express.

In this tutorial, we will learn how to build our very first Android based application using MonoDroid and Visual Studio 2010.

Installing MonoDroid

You can download the latest version of the MonoDroid from www.monodroid.net and follow the installation instructions here: http://monodroid.net/Installation

Once you have successfully installed the Android SDK, MonoDroid and all required software, you are all set to build your very first Android based application using C# and .NET.

Hello Android!

We are going to build our very first Android based application. In this application, we will display the Hello Android text on the screen.

Open Visual Studio 2010 and create a new project. Select Visual C# as the language, MonoDroid as the category in the left side bar list of the project template categories. See Figure 1.

Once you select the MonoDroid category, you will see the following three templates.

  • MonoDroid Application
  • OpenGL MonoDroid Application
  • MonoDroid Class Library

In this topic, we will use the MonoDroid Application project type. I will discuss OpenGL and Class Library project types later in this chapter.

MD1.gif
Figure 1

As you see in Figure 1, I select MonoDroid Application on the project templates, enter my project name HelloAndroid and click OK. This action will create a new project and add default files and code to the project as shown in Figure 2.

MD2.gif
Figure 2

The default file opened in the editor is Activity1.cs.

If you look at the code of Activity1 class listed in Listing 1, you will see there are six Android namespaces are added to the file. The default namespace of the project is the project name you created in Visual Studio.

using System;

 

using Android.App;

using Android.Content;

using Android.Runtime;

using Android.Views;

using Android.Widget;

using Android.OS;

 

namespace HelloAndroid

{

    [Activity(Label = "HelloAndroid", MainLauncher = true)]

    public class Activity1 : 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++); };

        }

    }

}

Listing 1

The Activity1 class is inherited from the Activity class. Each Android application must have at least one Activity. The class also has an overridden OnCreate method. This is the most useful method and gets executed when an application starts. You should write all initialization and UI related code here. Before we discuss this in more details, let's take a look at the other files in the project.

MD3.gif
Figure 3

If you look at the Solution Explorer more closely (See Figure 3), you will notice Assets, Resources and Values folders. You can also expand these folders to see what default files are added to the project. We are going to discuss these files one at a time.

Before we get into more details, let's add code for our Hello Android display on the screen. I change code of the OnCreate method and add a TextView object and set it's Text property to Hello Android!. The TextView object works as a TextBox control. It is used to display and manage text on the screen.

protected override void OnCreate(Bundle bundle)

{

    base.OnCreate(bundle);

 

    var tv = new TextView (this); 

    tv.Text = "Hello, Android!";

 

    // Set our view from the "main" layout resource

    SetContentView(tv);

}

Listing 2

The SetContentView method is responsible for pushing and displaying the contents to the screen.  

 

Now let's build and run the application. Select Build and Run menu item in Visual Studio.

First thing you will see is a screen to select devices. If you have an Android enabled device attached to your computer, you will see that listed here. I use the emulator for testing. To view all emulator images, click on the Start emulator image link on the screen.

MD4.gif
Figure 4

This action will load all the emulator images on your computer. As you can see from Figure 5, I have two emulator images on my computer.  

MD5.gif
Figure 5

Now click OK button. After that, you see a screen with various options that checks your emulator and deploys the latest application on the device.

MD6.gif
Figure 6

Once an application is successfully deployed, you can go to Applications and search for it. I search and find HelloAndroid application on my device. See Figure 7.  

MD7.gif
Figure 7

Summary

MonoDroid is a framework that is used to build Android based mobile applications using C# and .NET. In this tutorial, we learned how to get started with the MonoDroid framework and Android SDK and build and deploy a simple Android based application.

 

Up Next
    Ebook Download
    View all
    Learn
    View all