Building your first Android application

Setting up an Android application in Xamarin Studio is just as easy as it is for iOS. It's also very similar to the experiences in Visual Studio. Xamarin Studio includes several project templates that are specific for Android to jump-start your development.

Xamarin Studio includes the following project templates:

  • Android application: A standard Android application that targets the newest Android SDKs installed on your machine
  • Android Honeycomb application: A project that targets Android Honeycomb, which is API (Application Programming Interface) level 12 and above
  • Android Ice Cream Sandwich application: A project that targets Android Ice Cream Sandwich, which is API level 15 and above
  • Android library project: A class library that can only be referenced by Android application projects
  • Android Java bindings library: A project for setting up a Java library to be called from C#
  • Android OpenGL application: A project template to use low-level OpenGL for 3D or 2D rendering
  • Android unit test project: A project for running NUnit tests on Android

Launch Xamarin Studio and start a new solution. From the New Solution dialog, create a new Android Application under the Android section.

You will end up with a solution that looks something like the following screenshot:

Building your first Android application

You'll notice that the following files and folders specific to Android have been created for you:

  • The Assets folder: This directory will contain files with a build action of AndroidAsset. This folder will contain raw files to be bundled with an Android application.
  • Properties/AndroidManifest.xml: This file contains standard declarations about your Android applications, such as the application name, ID, and permissions.
  • The Resources folder: Resources are images, layouts, strings, and so on that can be loaded via Android's resource system. Each file will have an ID generated in Resources.designer.cs that you can use to load the resource.
  • The Resources/drawable folder: Any images used by your application are generally placed here.
  • The Resources/layout folder: This contains any *.axml (Android XML) files that Android uses to declare UIs. Layouts can be used for an entire activity, fragment, dialog, or child control to be displayed on the screen.
  • The Resources/values folder: This contains XML files to declare key-value pairs for strings (and other types) throughout an application. This is how localization for multiple languages is normally set up on Android.
  • MainActivity.cs: This is the MainLauncher action and the first activity of your Android application. There is no static void Main function in Android apps; execution begins on the activity that has MainLauncher set to true.

Now let's perform the following steps to run the application:

  1. Click on the play button to compile and run the application.
  2. A Select Device dialog will appear.
  3. Select the emulator of your choice and click on Start Emulator. If you have set up the x86 emulator in Chapter 1, Xamarin Setup, I would recommend using it.
  4. Wait a few seconds for the emulator to start. Once it starts, it is a good idea to leave it running as long as you are performing Android development.
  5. You should see the emulator now enabled in the list of devices; select it, and click on OK.
  6. The very first time you deploy to an emulator or device, Xamarin Studio will have to install a few things such as the Mono shared runtime and Android platform tools.
  7. Switch over to the Android emulator.
  8. Your application will appear.

When all is done, you have deployed your first Android application, complete with a single button. Your app will look like the following screenshot:

Building your first Android application
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.197.201