“Hello World” Application

It is very common in the programming world to build a “Hello World” application after the preliminaries such as the installations are out of the way.

CREATE NEW PROJECT

Fire up your Visual Studio and choose File > New Project to view the available Mono for Android project templates:

Note: It is advised to use only letters and numbers in your project/assembly name (with no dash, hyphen, etc.) in order to avoid any possible compilation error.

The project layout is very similar to the one in native Android projects in Eclipse IDE:

What is Mono.Android.dll?

Mono for Android applications are built against the Mono for Android Profile which is actually a subset of the .NET Base Class Library (BCL). This profile is packaged in the Mono.Android.dll, and contains almost the entire Android Development Kit (ADK). This is how Mono for Android  allows .NET developers to invoke Android APIs directly from C#.

By default, creating a new Mono for Android project comes with an activity file in C#:

What is Activity?

It is a class file that you place the logic of your application. It is like the code-behind part of an ASP.NET Web Form page. It is mapped to your UI (application screen) that you define in your layout. An activity inherits from the Android.App.Activity class.

This sample application includes a button, and the button’s caption will be updated after each click to demonstrate how many clicks have been made so far.

DEFAULT CODE SETUP

The way that the default code setup (activity and resources) is straightforward and doesn’t require any special explanation:

Files under the Resources directory (such as Main.axml and Strings.xml) include the application resources and XML based UI definitions that can be accessed by any activity file in the project.

When developing with Mono for Android, there is no visual UI designer. UI layouts are created by coding XML files like Main.axml. There are free UI designers out there though such as DroidDraw. You may find it useful to create XML based UI layouts for your Android applications in the future.

LAUNCH EMULATOR and DEBUG

Run the application as you always do in Visual Studio:

This initiates the build process, and you will be given the following dialog:

Please keep in mind that the trial version only allows you to deploy to the emulators.

Click on the “Start emulator image” link to view the available emulators:

Choose the emulator that you want to deploy your application, and click on the OK button. You will then be provided the emulator name in the list of running devices:

The emulator may take a while to launch itself. The good part is that you don’t need to close it if you want to redeploy your application after making some code changes.

After the emulator launches, slide the lock button to the right and get the home screen:

Note: An alternative way to launch the emulator is using the Start Android Emulator option under the Tools menu in Visual Studio. It opens the Android SDK Manager where you can view the available Android Virtual Devices (AVDs) through its own Tools menu.

Go back to your “Select Device” dialog, and click on the OK button in order to start deploying your application to the emulator. Your application will get packaged into a native Android application during this process. If this is the first time that you are deploying a Mono for Android application, Mono runtime and platform framework will be installed first. The runtime one is a large file, therefore you may need to wait for a while. Luckily, these installations occur only once per AVD.

Once all these steps are 100% complete with no issues, you will have your first Android application up and working in the emulator:

After each click of the button, its caption will be updated based on the logic that you have in the activity file (Activity1.cs) and will show how many clicks have been made so far.

CONCLUSION

This simple sample application showed how .NET developers can use Mono for Android to develop Android applications without having to learn Java and Eclipse which may need significant time investment. Certainly, a better understanding of Android architecture should also be learned in order to build your Android dream game.

About these ads

4 Comments

  1. Jonathan S. · · Reply

    I followed everything in this tutorial. When I tried to deploy my project with no change to your code and selected the emulator, I am always given the following error message:

    “Could not create the Android package. See the Build window for more details.”

    Can it be because of deploying with or without debugging?

    1. No, deploying with or without debugging shouldn’t matter. Can you post your build (output) window so that we or one of the other readers can take a look at it?

  2. I had exactly the same error because of a special character in the package name.

    1. Jonathan S. · ·

      Perfect.Thank you Raveesh!!!
      I changed my project name from Hello_World to HelloWorld, and it is now deploying without any problem.

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

%d bloggers like this: