Android applications in C# using Visual Studio

15. January 2011

I recently got myself an Android mobile phone and was really interested in writing applications for it. After downloading the Android SDK as well as the 32-bit Java SDK (noooooo!) I got it all installed and running the sample apps with Eclipse and browsed around the code.

Firstly, I wasn’t really too keen on getting into another development environment. Secondly, I really didn’t want to do any Java development if I didn’t have to. C# is my happy place, C++ is for the ugly stuff if you need it, Java is just, well, ugly... Besides the last time I did any serious Java was in college, enter MonoDroid to save the day!

Getting Started

To get everything installed read this guide. Then you can read about the API and architecture to get a better understanding about how it all gels together. Pretty much everything you need including signing up for the newsletter can be found by going directly to www.monodroid.net.

Once you have everything installed it’s pretty easy to get going. The beauty about diving in using a language you are familiar with, means you can get used to the Android SDK without having any knowledge of Java or even C++ (for more advanced features). MonoDroid wraps up all the calls using standard .NET constructs through a JNI bridge, plus, you get all of the power of the standard .NET API’s which work in Linux/Android.

Hello Android

Following the tradition of “Hello World”, let’s jump right in and write an Android application.

  1. Make sure that you have everything installed, this guide couldn’t have made it easier.
  2. Create a new Project in Visual Studio: New MonoDroid project in Visual Studio 2010
  3. MonoDroid does most of the heavy lifting for you. If you’re familiar with Android projects in Eclipse you should have any problems with the structure. Android uses layout and string resource files (the right way) for the UI. Activities are like UI controllers, all the info you need is on the Android Developers website.
  4. Just run the application to check everything is working correctly, if you don’t have a device attached or an emulator running then now is the time to create one: Select a device to connect the debugger 

    If you don’t see anything then just click “Start emulator image”: Select an emulator to start or create a new one
    Once again MonoDroid holds your hand by signing the package and also installing the Mono runtime if it’s not already available.
    WARNING: If you are running an emulator then this will take a while, if you have a monster PC that does it in seconds then I hate you.
  5. Unlock the phone/emulator and click the button: The default Hello World application 
    That’s it! There is a lot of documentation on writing Android applications. Java isn't that far off in syntax to C# so it will be easy to pick up sample code all over the place.

 

 

More Information

The best place to start is the MonoDroid API design documentation. Do yourself a favour and browse the classes in the Android namespace. Make sure you check Android.Provider which gives you access to things like Contacts, Settings, Calls Logs etc. through static classes.

This Justin guy slicked the domain www.mono-droid.com in July 2010 already so this isn’t new, just widely available now and free for the time being. There is a lot of useful information on installing and developing here. He’s got videos too, everyone loves videos.

 


Android, C#, Mono, MonoDroid, Visual Studio , , , ,

Comments (14) -

5/18/2011 9:55:59 AM #
Hi, good stuff here, i was looking for info about making android app using delphi.
Nilesh
6/11/2011 10:36:09 AM #
Pretty much everything needed to get started. Nice Article...Thank You
6/13/2011 8:17:40 PM #
Looks like it's not that difficult to get into Android development. I guess that is why the Android Market has so many apps. Thanks for the tutorial.
Soan
8/17/2011 4:32:34 PM #
Wow..i did not knew that we can develop Android apps using C# itself. But looks like the IDE of Mono is a paid one. Is there any other such tool available for free?
Soan
8/17/2011 4:34:24 PM #
Can we use .net DLLs also with Mono For Android? I mean embedding them in the bin folder just like we do in normal web apps?
Werner
8/17/2011 6:06:49 PM #
@Soan: MonoDroid is indeed a paid for product and rightfully so, it's very unique and build off Mono (free) bridge .NET on a seemingly unsupported platform. It is getting taken very seriously though and sponsored by Xamarin now providing professional support. I haven't seen a free product yet since you can already use Java or Native C++ to write Android applications, it's unlikely something like this will be free anytime soon.

All the same rules apply for Android (or rather applications executing on Mono) as they do in a Windows environment, web or desktop. Dependencies will always be searched for in the direct executing directory of the application, GAC, PATH and alternative configured probing paths. The idea behind Mono is to be able to deploy and run your application on a non-Windows platform without any significant changes. It even changes Windows path backslashes (\) to forward slashes (/) when running on Linux to maintain compatibility. As stated on the Mono site
An open source, cross-platform, implementation of C# and the CLR that is binary compatible with Microsoft.NET
means your app won't know if it's calling Mono framework assemblies, Microsoft .NET framework assemblies or your own custom built ones.
Raheel
11/3/2011 5:55:58 AM #
I love developing apps for Android but before coming through this post I have no idea that it is possible even with C# in Visual studio.you really gives me the thing I need most in my life.
Lynz
3/29/2012 7:50:21 PM #
My life is now complete! Laughing
Well, not exactly, but thanks for this awesome article! Gonna have tons of fun with this now.
comrade
5/3/2012 6:23:08 AM #
Yeah, sadly, it IS paid.... and its price is about 999 € or $ (I forgot)
I first thought it was bout 99, but noo, its a thousand bucks   D:
Fred BUtz
5/9/2012 11:07:37 AM #
It would be nice for a hobbiest version.  But it is too pricy.
Henry
5/31/2012 6:36:05 PM #
Thanks for your artical. It is good to know how to develop for that platform with .NET
Jurgen
6/10/2012 3:50:43 AM #
For personal use you can buy the professional license, $400.
Taersious
6/10/2012 10:53:03 PM #
I was so excited to find this article. Then only to find out I am going to be forced to fork over some serious kizash. No thanks! Plz let us know up front before providing a link off site "To get everything installed" that we are going to be dinged to install.
Werner
6/17/2012 5:59:49 PM #
At the time the article was written, MonoDroid was still open source and freely available. About three months later the project developers got canned and Novell bought them out. It was once again bought by Xamarin who decided to close the source and sell it some time after that.

Now it's only available as a trial and I don't mean to dupe anyone into buying it. Here's an article of when it all happened if anyone is interested: conceptdev.blogspot.com/.../...marin-what-now.html

Add comment