Before you build the application, you need to consider the template code and make some quick changes. The template code is as follows:
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace HelloAndroid {
[Activity(Label = “My Activity”, MainLauncher = true)]
public class Activity1 : Activity {
int count = 1;
protected override void OnCreate(Bundle bundle)
Visual Studio Development with Mono for Android ❘ 27
{
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++); };
} } }
This block of code shows a few things.
First are the using clauses needed for this code.
Then you have the namespace declaration that is set to your application name, HelloAndroid.
Then you have the class declaration for Activity1, which is of type Activity.
An Activity is central to the design of Android-based programs, and they are discussed more in upcoming chapters, particularly Chapter 3. However, the annotations on this class are also of note. First is the label My Activity, which will be the label seen in the Android application window. Second is the MainLauncher annotation, which indicates that this Activity is the main one to be launched in this application.
Finally, you have the OnCreate function. Activity creation is just one of several life cycle steps that an Activity may be subjected to. The whole life cycle will be discussed further in Chapter 3. In this function you initialize a resource bundle, set your view, get a button from the view, and attach an event to it.
Now you are ready to build the new application. Click the Debug button on the toolbar. You are prompted to select a running device to deploy the code to, as shown in Figure 2-6. You should see listed the emulator that you started running earlier. If there is no running device, you can select a device to start.
FIGURE 2-6
Select that emulator, and click OK. The Mono for Android toolkit then checks for an installed version of the Mono for Android runtime. If the runtime is not found, the toolkit installs it. This process can take quite some time. Once the runtime is installed, the toolkit signs and installs the application into the running emulator.
After that process has fi nished you can run your application. Go to the emulator, unlock it, and click the Applications button. You should see an image similar to Figure 2-7. Click the My Activity application. You should see the application running, as shown in Figure 2-8.
FIGURE 2-7
Logging
To follow the fl ow of the program execution, it is often helpful to log program activity. This section briefl y examines how you can implement logging messages in Mono for Android. The Log class can be found in the android.util namespace. You can add a few lines to the code you had before to get the following source:
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Util;
Visual Studio Development with Mono for Android ❘ 29
namespace HelloAndroid {
[Activity(Label = “Hello Android”, MainLauncher = true)]
public class Activity1 : Activity {
int count = 1;
protected override void OnCreate(Bundle bundle) {
Log.I(“HA”, “Start OnCreate”);
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++); }
Log.I(“HA”, “End OnCreate”);
} } }
FIGURE 2-8
Here you can see the added using Android.Util that provides access to the Log class, which contains the following convenience functions (among others):
Log.I(string tag, string message) logs information.
Log.W(string tag, string message) logs warnings.
Log.E(string tag, string message) logs errors.
The tag parameter provides context for the log message. In this case you can use a tag of “HA” for HelloAndroid. To view the messages in Visual Studio, select View Other Windows Android Device Logging, and all the messages will be available.
Debugging
Having successfully executed the application in your emulator, you can look at how to debug a problem that we will introduce. If you are using a physical phone, you need to go to the applications page on your phone and select Settings. Then select Applications, Development, and check USB Debugging. After that, return to your code.
Change the following line:
Button button = FindViewById<Button>(Resource.id.myButton);
To the following:
TextView button = FindViewById<TextView>(Resource.id.myButton);
Rerun the application. This time the application will throw an error on start-up because you are try-ing to treat a Button as a TextView. While this example may be contrived, take a look at how you can debug the application.
Set a break point on the following line:
base.OnCreate(bundle);
Now, click the run/debug button on the toolbar. This time, as the application starts up the software will stop at the break point. You can now step through the application until you arrive at the offend-ing line. Tryoffend-ing to step over that instruction will result in the previously seen error, and, in this case, fi xing it is trivial.
Testing
The days of merely testing software through usage are long gone. All reliably built software relies on unit tests as a best practice and to make the testing cycle shorter and more reliable. So, how do you build unit tests with Mono for Android?
The short answer is NUnit, just as it is for any other Mono application. The longer answer involves structuring your program to make it amenable to testing. That is, the NUnit testing framework is not geared toward UI testing, so it is best to isolate your non-UI code into a separate library and set up any tests to run against that library.
It is also worth noting that if you intend to leverage code for other platforms, for example, the iPhone with MonoTouch or Windows Phone 7 with Mono or .NET, then you also want to isolate platform-specifi c code from generically reusable code. This code would also be good code to build test cases against.