Android Application Development – Part 1


17 June 2011, by

Introduction

These articles are intended as a quick-start guide to getting yourself up and running developing Android applications. The idea is to provide a quick guide that follows the development of an application touching a few more areas of the system than the basic ‘Hello World’. It’s aimed at developers familiar with Java who want to learn more about developing for Android.

The app we’ll be developing is a simple GPS application, able to request and read some coordinates from GPS hardware aboard the Android device, make use of a web-service to derive some information about the location (this is called reverse-geolocation), and display what it has found out.

Overview of an Android app

Android apps are made up of the following components:

  • Activities (extend Activity) – these are the UI, and contain a hierarchy of Views (a view could be a button, a field, or a grouping of other views).
  • Services (extend Service) – these run quietly in the background, doing what they need to do. Services should spawn their own thread for anything resource/time consuming.
  • Broadcast receivers (extend BroadcastReceiver) – these receive messages (inherited from the class Intents) and act on them. Sometimes this is as simple as displaying a notification or setting an icon for the user to notice – use the NotificationManager class for this.
  • Content providers (extend ContentProvider) – these provide data to other apps and processes. They are found and communicated with through the ContentResolver class.

These components are defined in the app’s manifest file:

  • The Manifest – this is an XML file defining each component, and details about how it can be used, launched, etc. Amongst other things, the manifest defines the entry point to your app (i.e. Which Activity gets launched by tapping the application’s icon…)

Getting started

Setting up your development environment

  1. Download the SDK: http://developer.android.com/sdk/index.html
  2. Install the SDK, following instructions here: http://developer.android.com/sdk/installing.html. The installation instructions also cover steps to creating a target – which is a device you’re going to emulate.
  3. Create a target which has (as a minimum) a touch screen and GPS.

Shortcut: You can get straight to the Android SDK management application by clicking the icon on your toolbar in Eclipse:

The Android Eclipse Toolbar

Creating a new application

Start your new application as the simplest app you can using the New Android Project Wizard. As with every new project, choose File / New / Project… and select ‘Android Project’. When offered, select Create Activity, and name your new activity GpsMonitoringActivity. Here’s quick overview of such an app:

The Android Package ExplorerAndroidManifest.xml (highlighted) is the manifest file for the project. The SDK provides some helpful screens for editing the manifest file. You shouldn’t need to edit the XML directly for now, although that option is available.

Double click the manifest to view it, and note that you can switch between the various manifest editing screens using the tabs provided along the bottom edge. The last tab allows you edit the XML directly.

GpsMonitoringActivity was created by the Wizard, and is the initial activity for the app (and defined as the entry point).

main.xml (inside layout) is the layout used by GpsMonitoringActivity.

Modifying the view

We’re going to edit GpsMonitoringActivity’s view (“Main”) – which was provided by the Wizard.

Right-click main.xml, from the “Open with…” selection, choose “Android Layout Editor”. The layout editor offers two views: raw XML and a GUI designer.

For a comprehensive review of Android UIs, see http://developer.android.com/guide/topics/ui/index.html.

Android Views are managed by Layouts – which arrange UI items for us. We are going to make use of a simple Layout, the LinearLayout, to display a few pieces of text. The LinearLayout arranges items one after the other.

Find a TextView in the toolbox provided by the designer, and drag it onto the Layout. Because this layout has a LinearLayout as its root component, the TextView appears below the “Hello World” TextView already present.

Switch to the XML view, and you can see your TextView as it was created. Edit its properties to make it more clearly yours:

<TextView android:text          = "this is TextView01"
          android:id            = "@+id/TextView01"
          android:layout_width  = "wrap_content"
          android:layout_height = "wrap_content">
</TextView>

Note a couple of things:

  • If android:text had started with the prefix [email protected]/” then everything after the slash would have been an ID to look up in the application’s resource file. As it doesn’t have this prefix, it is treated as a literal string. (If your resource ID wasn’t found in the resource file, you’d see the content of android:text in your text field.)
  • Always prefix the android:id field with “@+id/” – this is very helpful, and as you will see in a moment, it causes the SDK to automatically include this id in the resources file.

For more information on resources – see http://developer.android.com/guide/topics/resources/providing-resources.html and http://developer.android.com/guide/topics/resources/accessing-resources.html.

Modifying the activity

Let’s go and edit GpsMonitoringActivity.java. Use the following code, and then we can discuss it:

public class GpsMonitoringActivity extends Activity {

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    TextView v = (TextView)this.findViewById(R.id.TextView01);
    v.setText("Starting...");
  }
}

Android is careful about creating and destroying views between uses. By default, the onCreate method is called when an activity is created – not when it is viewed again later. The properties that control this are editable from the manifest, although we will not cover this now.

The first line of interest is: setContentView(R.layout.main);

  • This line was auto-generated by the android application wizard.
  • R is a class representing the resources available to this application, and was auto-generated by the SDK/Eclipse environment. It contains all your layouts, ids, and strings.
  • We’ve left the view set by this activity as the “Main” view (R.layout.main).

The next line is interesting too: we are using findViewById, and the id now available from R, to get hold of the TextView we recently added to the view. Once we have this it is a simple matter to set the text on the view programmatically to “Starting…”.

Quick test

Run the emulator now to try it out. Create yourself an Eclipse run configuration for the android target if you want, and hit the play button.

Time saver! It takes an age for the emulator to start (for me, anyway) – so it’s worth noting that if you haven’t closed down the emulator, you can push your new version of the app to it by hitting play again without restarting the emulator.

Next time…

Part 2 will cover getting hold of GPS data whenever it is updated, and displaying it in the TextView…

next article in series

Tags: , , ,

Categories: Mobile, Technical

«
»

One Response to “Android Application Development – Part 1”

  1. Nick Johnson says:

    hey thanx for the post man….
    You provided very useful information. It will be useful for every android developer in the long run..
    Keep posting such informative posts….


Leave a Reply

* Mandatory fields


2 × = twelve

Submit Comment