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 and follow the installation instructions here:
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.
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.
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.
  1. using System;  
  2. using Android.App;  
  3. using Android.Content;  
  4. using Android.Runtime;  
  5. using Android.Views;  
  6. using Android.Widget;  
  7. using Android.OS;  
  8. namespace HelloAndroid  
  9. {  
  10.     [Activity(Label = "HelloAndroid", MainLauncher = true)]  
  11.     public class Activity1 : Activity  
  12.     {  
  13.         int count = 1;  
  14.         protected override void OnCreate(Bundle bundle)  
  15.         {  
  16.             base.OnCreate(bundle);  
  17.             // Set our view from the "main" layout resource  
  18.             SetContentView(Resource.Layout.Main);  
  19.             // Get our button from the layout resource,  
  20.             // and attach an event to it  
  21.             Button button = FindViewById<Button>(Resource.Id.MyButton);  
  22.             button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); };  
  23.         }  
  24.     }  
  25. }  
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 the initialization and UI related code here. Before we discuss this in more detail, let's take a look at the other files in the project.
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 the code of the OnCreate method and add a TextView object and set its Text property to Hello Android!. The TextView object works as a TextBox control. It is used to display and manage text on the screen.
  1. protected override void OnCreate(Bundle bundle)  
  2. {  
  3.     base.OnCreate(bundle);  
  4.     var tv = new TextView (this);   
  5.     tv.Text = "Hello, Android!";  
  6.     // Set our view from the "main" layout resource  
  7.     SetContentView(tv);  
  8. }  
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.
The 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.
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.
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.
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.
Figure 7


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.

Similar Articles
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.