Aug 182012
Article Android

In our previous post on how to develop apps for android devices we explained how to set up an environment for the development of applications targeted to this platform, using the Eclipse IDE and the ADT plugin. In this post we will analyze the concepts and elements that make part of an Android app.

Create a new Android project

We’ll start by creating a new Android project in Eclipse, selecting File > New > Project > Android > Android Application Project.

The creation of the project displays a series of dialogs:

Application and project name – These two are usually the same (The project name is only used internally by Eclipse, and must be unique).

The java package name (In our example, “com.openalfa.hello”)

The SDK version to use in building our project. In the dropbox, we can choose from any of the SDKs previously installed with the SDK Manager.

The minimum API version required to run our application.

The icon to represent our application.

The main Activity (see below for an explanation of the meaning of “Activity” in Android applications).

Activities, Controls and Layouts

An Activity is a java class that contains all the logic required to carry out a certain task in our application. Taking for instance an email reader application, we can think of a series of tasks that are typically performed by that kind of application, that would be implemented as Activities:
  • download from the email server the new incoming messages
  • display the list of messages in the Inbox
  • display the content of a message
  • Answer a message
  • Write a new message
  • etc…

In most cases, performing an Activity requires some interaction with the user. To make this interaction possible, the Activity creates in the screen a series of graphical elements generically referred to as Controls: Text boxes, Buttons, Form Fields, Images, etc…

The location of these controls on the screen is determined by a set of container LayoutsLayout is the generic name applied to Views and ViewGroups, where:

  • View contains a set of controls.
  • A ViewGroup contains a set of Views.

A simple application will be implemented in a single Activity, and a single Layout including one or several Controls.

Example Activity

In the “Hello” project just created, the Activity is written in the file “src/com/openalfa/hello/, and its content is:

As we can see, the Activity MainActivity extend from the generic “Activity” class. In the generic class there are default placeholder methods for all the possible types of events that can be generated in an Android application.

The programmer must replace (using the @Override keyword) these placeholder method with methods that implement the desired functionality. In our application, only the onCreate and onCreateOptionsMenu methods are overriden. The onCreate method is responsible for the initialization of the application. In our case, it just makes a call to “setContentView” to display the “activity_main” layout.

Layout Example

The “activity_main” layout used by our application resides in the file “res/layout/activity_main.xml”, and its content is:

We can see that it defines a single layout covering the whole screen (the with and height values are “match_parent”). Inside the layout there is a single “TextView” element. This text box is initialized in line 11 with the string literal “@string/hello_world”.

Layout edition and types of Layout

The ADT plugin for Eclipse provides a graphical editor for layouts, that is invoked when the .xml layout file under the “res/layout” directory is selected. In our example, after selecting “activity_main.xml” in the left pane, the editor looks like this:

Editor de layouts Android del Eclipse IDE


The areas enclosed in red boxes in the above image are:

  1. Zone where the current layout is rendered
  2. Palette, where we can choose different layouts and controls to be incorporated to the layout design
  3. Tabs to switch from the graphical editor to the xml text editor and vicecersa
  4. Selector for the size and resolution of the screens/devices we are targeting
  5. Selector to switch between portrait and landscape orientation
  6. Tree View of the hierarchical structure of layouts and elements added to the screen.

The most common types of layouts are:

Linear Layout: Each control added is put to the right of the previous one (horizontal layout), or below (vertical layout)

Relative Layout: Every control is positioned relative to the screen corners, to the screen center, or to the position of other elements.

In the next image we can see the result of adding some button controls in these three layouts:

In the following posts we will explain how an Activity interacts with the controls, and how it can be programmed to respond to events generated by the user.

 Posted by at 8:47 am

  One Response to “Structure of an Android application”

  1. Nicely explained. Thank you!

 Leave a Reply