Getting started with any new technology that you are not familiar with can be a little daunting so I wanted to write a little tutorial to help people get a head start and to pass on the things that I have learned through trial and error.
A Little Background on Mono
The Mono Project was started by Miguel de Icaza as an open source project on July 19, 2001 while working at Novell. The intent was to support the .NET Framework on Linux systems to help increase developer productivity. After Icaza was laid off when Novell was acquired by Attachmate in April 2011, he started the company Xamarin. This company was given a perpetual license and stewardship of the Mono, MonoTouch, and Mono for Android projects.
Setting Up Your Environment
You can go to the Xamarin installation documentation for detailed information on how to set up your development environment. The process is fairly straightforward and really just requires that you download the installer which will install all the required components for you.
After you have run the installer you will have the Trial Version which will allow you to develop Mono for Android applications and deploy them to the emulator for testing. In order to deploy the application to a device you must buy a license and activate it.
Creating Your First Mono For Android Application
The first step will be to open Visual Studio and create a new Android Application project.
Lets go ahead and name this project “HelloMonoDroid” and click OK.
Now we have a basic project that we we can begin to work with. Let’s start out by examining the structure of the Project
- Properties Folder : Contains the AssemblyInfo.cs file where information about assembly and its permissions on the phone are located.
- Reference Folder: Lists dlls that the application needs for functionality.
- Assets Folder: Contains assets that the application needs; Text Files, Fonts, etc…
- Resources Folder: Contains sub folders and files for resources as well as an autogenerated file to access these resources.
- Drawable Folder: Contains image files used in the application.
- Layout Folder: Contains .axml files that are used as designers for the different Views in the application,
- Values Folder: Contains the Strings.xml file which is used for localization of the strings used in the application.
- Activity Files: The logic for each screen of the Android application
The two files that we need to work with for the HelloMonoDroid application are the Main.axml and the Activity1.cs file.
The Main.axml file is the default designer file that the application will use to render a View when a new Android Application is created. This file can be renamed to whatever you would like and additional view can be created to work with this or replace it. A view can be a single control on the screen, the entire screen, a dialog, a menu, a list, etc… Because of this, for every View that we want to see, we need to create a new .axml file that corresponds to that View. For now we will just be creating a single View that will correspond to a single screen.
The Activity1.cs file is the generic name that is given the default Activity when an Android Application project template is used. The Activity acts as the controller for the application. An Activity can be thought of as the logic for the screen that is being displayed. The Activity uses the .axml file to render the controls that will be displayed on the screen and it is here that you can programatically interact with the controls and add the functionality that make the application go.
So lets get started.
Running The Default Application
The Application that we will be making is a simple adding application that allows the user to enter a number in a text box, click an add button, and display the sum. We will also have a reset button to set the summed value back to zero.
Setting Up a Clean Project
First we will clean out our project to remove the default application content. I want to demonstrate to you guys how to create an application from scratch without the default files for the View and the Activity so we will go ahead and delete the Main.axml and the Activity1.cs files first.
Now that we have a clean project to start from we want to begin by creating a new View Layout (.axml file).
- Right click on the Layouts folder and select a Add New Item.
- From the Add New Item Dialog, select Android Layout and name it StartScreen.axml.
Depending on the version of Mono for Android that you have downloaded, you will get either a graphical designer or a xml file. I prefer to use the xml file for my design, and it gives you a clearer understanding of how to create a view, so that is how I will proceed.
If you have the graphical designer showing in your main window, you need to…
- right click on the StartScreen.axml file in the solution explorer
- and select ‘open with’ from the context menu. Then select Automatic Editor Selector (XML) from the dialog and click OK.
You will see the markup for the view in the main window.
*In a future blog I will do an in depth look at the View and take a look at most of the controls that you can work with, but for now we will keep it simple and confined to this demo.
Building The Screen
Like we talked about a little earlier, the Screen is represented by the axml file that is loaded by the Activity. We need to add the controls that we want on our screen to display. For this demo, that is a EditText for to allow the user to enter a number, a TextView to display the sum total, and a Button to add the number in the EditText to the sum.
(I will complete this post in the near future… in the meantime, here are the code sample from IPMA 2012 forum.)
Mono Development (w/ Android) powerpoint slide-deck
Mono Source-Code examples