Saturday, November 12, 2011

Creating your First Android Application

From a .Net Developer's Perspective

I will be showing you how to create the same application that was shown in the tutorial titled 'Creating your First Mono for Android Application'. Over time, I will create more advanced Android application development tutorials using both the Mono for Android (using C#) and Eclipse Android Plugin (using Java) frameworks.

Ensure that the 'Android SDK Tools' and ''Android SDK Platform-tools' are fully installed.

I also installed 'Android 2.2 (API 8)' and 'Android 2.3.3 (API 10)'.

Click on the 'Install N packages...' when ready.

Check the 'Accept all' radio button, then click on 'Install'. It will download and then install the packages. Please note that this download & installation process takes a long time to complete. Don't worry if the log shows an "'adb kill-server' failed -- run manually if necessary." error message. This is simply the package manager's attempt to stop the Android Debug Bridge's server process, which is not yet/already running.

Click 'Yes' to restart ADB.

Close the SDK manager.

Configure the Android Simulator:

Open up the Android SDK AVD Manager.

Click on 'New...'.

Enter MonoDroid in the 'Name' field.

In the 'Target' drop-down, select 'Android 2.2 - API Level 8'.

Choose 512MB for the 'SD Card' 'Size'.

Select 'HVGA' as the 'Built-in' 'Skin'.

In the 'Hardware' section, click on the 'New ...' button.

Select the 'Device ram size' property from the drop-down list.

Click on 'OK'.

Enter 512 for the 'Device ram size' property.

Click on 'Create AVD'. It will take about a minute to complete.

Close out of all Android SDK windows.

Install the Android Development Tools (ADT) Plugin for Eclipse:

Launch the 'eclipse.exe' file from the Eclipse folder.

Specify your Workspace location. This is the folder in which Eclipse stores your Eclipse projects. I've set mine to C:\Documents and Settings\Mike\My Documents\Eclipse.

Browse to and select the location of the Android SDK installed previously. I've set my 'Existing Location' to C:\AndroidSDK.

Click on 'Next'.

I've opted not to send usage statistics to Google.

Click on 'Finish'.

Creating the First, Hello World Android Application

If it's not already open, launch the 'eclipse.exe' application.

Click on the 'File' menu, then go to 'New' and click on 'Project'.

Open up the 'Android' folder and select 'Android Project'.

Click on 'Next'.

In the 'Project Name' text box, enter HelloWorld.

Click on 'Next'.

Select the 'Android 2.2' SDK target.

Click on 'Next'.

In the 'Package Name' text box, change the name to net.DotNetFun.HelloWorld.

In the 'Create Activity' text box, change the activity to MainActivity.

Click on 'Finish'.

Close the 'Welcome' tab in the upper-right corner.

Rearrange the workspace so the panes are more appropriate spaced. This is what mine looks like:

In the 'Package Explorer', expand the 'HelloWorld', then expand the 'res' and 'layout' folders.

Double-click on the 'main.xml' file to open it. This file is equivalent to the Main.axml XML file used in the various Mono for Android Visual Studio project templates. It defines, declaratively, the layout for one or more activities. For more info about declaring XML layouts, visit this URL: http://developer.android.com/guide/topics/ui/declaring-layout.html

Double-click on the 'strings.xml' file to open it. This is equivalent to the the 'Strings.xml' file used in the various Mono for Android Visual Studio project templates. It defines, declaratively, string resources. One difference you'll notice, however, is the 'app_name' resource. The Eclipse project requires, by default, this resource exist. For more info about declaring string resources, visit this URL: http://developer.android.com/guide/topics/resources/string-resource.html

In the 'Select a preferred Android Virtual Device for deployment' list, select/check the 'MonoDroid' emulator created earlier.

Click on 'Apply', then click on 'Close'.

Press F11 to debug and start the Android application. If you get a dialog box stating it's waiting for the debugger to attach, and it doesn't go away after a while, you'll need to restart Eclipse, re-check your debug configuration, and press F11 again.

Slide over the button to get to the Android desktop, then click on the applications icon/button.