Thursday 5 January 2012

First View of Android app structure behind the Scenes





Well I thought XAML and c# could be complicated, but the pattern that holds the Android UI together is no simple walk through the park either. And having not looked at Java in years, I presume it is similar for other Google Apps? Cryptic is not the word. The basis for the elements to be rendered is fair enough. It's XML and I like the render tool that is built into the the eclipse plugin for Android. But the model for linking it to the Java code behind the form is not intuitive and requires you to burrow down through resources to see how it all comes together and all this from a resource reference in the default Activity stub called R??

We go from this

public class Project1Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}

which is fair enough to this which is the equivalent of .g.s.cs hidden code generated file in VS

package com.appsolo.adnroid;

public final class R {
public static final class attr {
}
public static final class drawable {
public static final int ic_launcher=0x7f020000;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class string {
public static final int app_name=0x7f040001;
public static final int hello=0x7f040000;
}
}

which is not

and then we are down to the resources which I know XAML can rely heavily on as well

here is the XML for the form which is rendered in the tool

xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello" />

LinearLayout>


The XML is rendered nicely in the Graphical Tool of the Android plugin






















Although there is a Android Resources values tool for keeping track of values














But there are a lot of Dots to join up here!!!



First Steps in Android

Ok. I Got the Android development kit installed on a Mac. Thats a plus for a start. It runs native, raw and wriggly on the Mac. The Mac book pro being a fine piece of hardware and the Apple software including the OS being lovely to use but awkward to work with and find your way around for Windows head like myself.

For the life of me I cannot fully remember the process as I never can when installing downloaded software, some you do yourself and some comes down as a result of what you do. Suffice to say the standard installation instructions are for installation on a Linux or Windows machine. I had to follow another few links to get the proper tools for OSX 10. Here is the gist of what was needed.

You need

The JDK 7 for OSX from Oracle. When you unbundle this and install it. It runs an may pull down additional updates for the Mac.

Download and install a suitable version of eclipse. I took the Mac version from HEANET in Ireland.

Then its over to the Android SDK download page for the the Android SDK and AVD Manager. Which when unzipped have to be added to the eclipse environment.

Eclipse is the SDE of choice. First impressions of eclipse are it's not bad for free, but shows all the signs of it. VS 2010 is miles ahead and I have always said MS should push this more. However when I installed as per instructions the Android SDK Manager plugin for eclipse it did have a download manager that fixed a lot of interdependencies that were missing.

I then had to set up a AVD Android Virtual Device and attach it to the currently open Workspace.

AVD manager

It did not register with eclipse and the Hello World App did not deploy to it until I shut down eclipse and started it up again. But following a reboot of eclipse (on intuition) up came the emulator. The AVD is very slow to load and I'm using a SDD and Macbook Pro with 8gigs of memory.

Next challenge was to figure out the structure of a Java project (as it has been a while since I have seen eclipse and how to load an existing sample Android app that comes with the Android SDK.

Cmd + n creates a new project. You then choose what type of project you want The basis for eclipse is a workspace that is a enclosing folder that holds the project folders. A project must have a Package name otherwise it will not create. Which is similar to a name space in c# but is represented as a directory structure of the project folder in the workspace. The workspace contains a collection of project folders.

Project setup window

Once the Android SDK plugin is installed you you have access to the sample projects which you can then base a new project on. Right click on the project window



Followed two screens later by your Android sample template



Well thats the development environment  set up and ready to go. Now on to look at the Android in more detail.