Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

MonoDroid: How to build Android based Applications with C#

Making use of C# and ASP.NET, the article is used to laid down the foundation to come up with a Android based application making use of MonoDroid and Visual Studio.

Introduction to Android Based Applications

One needs to have Visual Studio in order to install and work with MonoDroid that works as an add-on of Visual Studio. You will be able to see the MonoDroid project templates in Visual Studio once this is installed successfully. The point to note here is that you won’t be able to able to have MonoDroid working with Visual Studio Express.

Building the Android Application!

After the successful installation of Android SDK , MonoDroid and all pending software, you are ready to build the Android based application making use of C# and .NET. Here we will be displaying the text as “Hello Android” on your screen.

Let’s look at the steps to create the Android based application with C#.

  • First open the Visual Studio > Create a new project > From the window, select Visual C# as a language and MonoDroid that Is present as one of the category in the left panel list where in all the project template categories are listed.
  • After going through the above case i.e. post selecting the MonoDroid category, the following templates will be displayed – MonoDroid, OpenGL MonoDroid Application and MonoDroid Class Library
  • Here in we will make use of MonoDroid Application type of project.
Selecting a template in Visual Studio

Figure1: Selecting a template in Visual Studio

As listed in figure 1 above, you need to select MonoDroid Application that is present amongst the project templates. You can enter the project name as “HelloAndroid” and then press OK.

After this step, you will be able to create a new project and incorporate in the default files and code to the project as displayed in the figure below.

Incorporating the New Project in the Default Files and Code

Figure2: Incorporating the New Project in the Default Files and Code

Activity1.cs will be the default file that you will be able to see in the editor. Looking at the Activity1 class listed in the below listing, you will be able to see that there are six Android namespace that are incorporated to the file. You can give the default namespace of the project that you have given in the project name that was actually created in Visual Studio.

Listing1: Adding six android namespace

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++); };
        }
    }
}

Activity class is used to inherit the Activity1 class where in each and every Android application ought to have at least one Activity, this class has an overridden OnCreate method as well. This is regarded as one of the most useful method where in it will be implemented and executed at the time of initiation of the application. All code related to initialization and UI should be written over here. The below figure represents some of the other files that are present in the project:

Files present in the Solution Explorer

Figure3: Files present in the Solution Explorer

One can closely look at the solution explorer to locate for the folders namely Assts, Resources and Values. One can expand these folders in order to drill down the list of the default files that are added to the project. We will be coding the Hello Android Application to be displayed on the Android based device. Here in we have changed the code of the OnCreate method and then incorporated a TextView object where after its Text property is set to Hello Android!

Listing2: Coding the Hello Android Application

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);
}

We have SetContentView method that holds the responsibility to push and display the contents to the screen of your device. Time has come now to build and then run the application for which you would be requiring to select Build and Run menu item in Visual Studio. There will be a screen displayed to you so as to select the devices. In case you have a device that is Android enabled and attached to the machine you are working on i.e. computer, the same will be listed here. For this article purpose, the emulator was used where in, to see the image, the need is to access the Start image link that is present on the screen.

Emulator reflecting the screen to Select a device

Figure4: Emulator reflecting the screen to Select a device

This will lead to the loading of the emulator images on the machine you are working on. The below lists the two emulator images that are present on my machine or computer.

Loading of emulator images on the computer

Figure5: Loading of emulator images on the computer

You need to access the OK button now post which there will be a screen displayed with good number of options. This verifies the emulator you are running and then deploys the latest application on Android device.

Android Emulator searching for the recent Android applications

Figure6: Android Emulator searching for the recent Android applications

After the deployment of the application, one can just navigate to Applications and then search for the same. The below figure lists the HelloAndroid application displaying on the Android device:

HelloAndroid Application displaying on the Android Device

Figure7: HelloAndroid Application displaying on the Android Device

Conclusion

We learnt in this article about the framework of MonoDroid that is helpful in coming up with an Android based applications that make use of C# and .NET.

I hope you liked the article and please see my others articles at mrbool.com in http://mrbool.com/space.asp?id=312860. Hope my articles are helpful and see you next time.



Software Developer from India. I hold Master in Computer Applications Degree and is well versed with programming languages such as Java, .Net, C and C++ and possess good working knowledge on Mobile Platforms as well.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login