Knowledge is power. We love to share it.

News related to Mono products, services and latest developments in our community.

imarusic

Hello world from Android

05/18/2012

In this simple tutorial I'll show you how to write a simple Hello world application using Mono for Android framework and some of its key features.

Mono for Android features

Nice thing about Mono for Android for .NET developers is that you can start writing application using C# and its well known features: delegates, events, anonynmous methods, LINQ... Also there is support for working with Java, C and C++ libraries, dynamics language features, lambdas, LINQ, parallel programming, base class library which contains great collection of classes to work with XML, database, IO, networking, strings...

Writing your first Hello world application

More details on how to install Mono for Android can be found here. The following steps will show you how to create a new project and write a simple Hello world application using MonoDevelop.

1. Create a new solution.

2. Select Mono for Android Application

Below is a simple Hello world example. As you can notice your class derives from the base Activity class. Activity is a main building block for Android applications. You can imagine it as a Page in ASP.NET, and it also has its life cycle (more can be found here.) Note that OnCreate method was overriden and used to attach to the button's event and set its behaviour. That method is called when the activity is created and it is a good place to make some initial settings and bind data to your controls.

using System;
 
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
 
namespace MonoAndroidTest
{
    [Activity (Label = "MonoAndroidTest", MainLauncher = true)]
    public class Activity1 : Activity
    {
        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);
 
            SetContentView (Resource.Layout.Main);
 
            Button btn = FindViewById<Button> (Resource.Id.btn);
            TextView lbl = FindViewById<TextView> (Resource.Id.lbl);
             
            btn.Text = "Click Me!";
             
            btn.Click += delegate {
                lbl.Text = "Hello world"; };
        }
    }
}

In this sample application I did not use an ordinary way to add a UI elements by instancing them directly in code. Instead I used XML and by calling FindViewById ("someId") I was able to get reference my UI element and manipulate them in any way I want. An example of XML is below:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <Button
    android:id="@+id/btn"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
    <TextView
    android:id="@+id/lbl"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
</LinearLayout>

All the code is ready so we can run our application and see the result. You can use Ctrl + F5 to run the application, select emulator. You should get the following screen:

This was very simple Hello world application using Mono for Android. I used a really basic stuff from this awesome framework and MonoDevelop development environment - new project creation, using XML for user interface creation and working with activities.

Rated 2.11, 9 vote(s). 
This would make more sense if you said where this code is supposed to go. Otherwise you are just showing off.
imarusic
Hi Barnett, thanks for the updates, I've fixed it. Which code are you referring to? This example is related to MonoDevelop.
Well, my Mono screen shows a list on the left-hand-side, which includes:
References: Mono.android, system, system.core, system.Xml.
Then there's Assets: about assets.
Then Properties, Android manifest.xml, assemblyinfo.cs
Then Resources/drawable/Icon.png and layout/main.axml and Values/strings.xml, about resources.txt and resource.designer.cs
Then activity1.cs
I can't even get a view of the screen layout, I must be clicking the wrong things, and when I run my new, empty project on the emulator, it says "hello World"