Saturday, November 16, 2013

Android: Creating a First App

Today's post is about the creation of a first Android app using Xamarin.
For that we open the Xamarin studio and select File - New Project - C# - Android Application.
What are forms for example in Windows Forms applications, activities are in Android, which represent a window of the application. Therefore, for us the file "MainActivity.cs" is important, since it contains the code of the main window (and so far only one) of the application.
We see, that the activity is represented as a class (MainActivity) like in Windows Forms applications . After the creation of the project this already contains the funtion OnCreate(). This is called, when the app is started. For demonstration purposes the function is already filled with code, which we delete, except the first line (base.OnCreate (bundle);).
We now want to create a simple application consisting of a textfield and a button.
Therefor we first have to create a layout to determine, how the controls should be arranged on the window. We here use a LinearLayout, in which controls are simply placed vertically or horizontally next to each other:

var Layout = new LinearLayout (this);
Layout.Orientation = Orientation.Vertical;

Layouts and controls expect in their constructor a so called context, here we pass over this for the current acticity.
Now we create the controls via code, a Label is here called Textview:

var TextView1 = new TextView (this);
TextView1.Text = "Hello Android";
Layout.AddView (TextView1);

As you can see, in the last line we add the control to the layout.
Adding the button is done analogously, just that we use an anonymous method to create an event handler for the Click event:

var Button1 = new Button (this);
Button1.Text = "Click me";
Button1.Click += delegate {
     TextView1.Text = "Button clicked";
};
Layout.AddView (Button1);

Eventually, we have to tell the activity which layout to use:

SetContentView (Layout);

With that, the app is ready for running. The complete code:

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace FirstApplication
{
     [Activity (Label = "FirstApplication", MainLauncher = true)]
     public class MainActivity : Activity
     {
          protected override void OnCreate (Bundle bundle)
          {
               base.OnCreate (bundle);

               var Layout = new LinearLayout (this);
               Layout.Orientation = Orientation.Vertical;

               var TextView1 = new TextView (this);
               TextView1.Text = "Hello Android";
               Layout.AddView (TextView1);

               var Button1 = new Button (this);
               Button1.Text = "Click me";
               Button1.Click += delegate {
                    TextView1.Text = "Button clicked";
               };
               Layout.AddView (Button1);

               SetContentView (Layout);
          }
     }
}

The app then can be started via Execute - Start (without) Debugger. For that either a connected cellphone or an emulator has to be chosen. I did not configure any emulators, but always test on my phone. Xamarin loads the app to the phone, where it is automatically executed.
To copy the app manually to a phone, it has to be compiled as a release version (Project - Active Configuration - Release), then the file "AppName.AppName-Signed.apk" from the release folder can be copied to the desired target directory. Android only allows signed apps to run, which is already done by Xamarin in the "Signed" file. If the app is to be published in a wider field (for example on Google Play), it should (must) be personally signed though.

No comments:

Post a Comment