Building the SaySomething Android application
This section creates a basic Android application, called SaySomething, using the Android Developer Tools. Once the application is created, we will debug and run it.
The first step is to create a new project. Select the wizard for Android project, as shown below.
Figure 2. New project wizard
The requirements for the application are:
- Name
- Location
- Package name
- Activity name — Think of this as the main "form" or screen of the application
- Application name
Take a look at the new project.
Figure 3. New Android project
This will create a default application ready to be built and run. The components may be seen in the Package Explorer, which we discuss next.
The Package Explorer (found in the Java perspective in Eclipse) displays all the components of the sample Android application (see Figure 4).
Figure 4. Package Explorer
Items of note include:
- src folder
- Includes the package for the sample application, namely
com.msi.ibmtutorial
- R.java
- The Android Developer Tools create this file automatically and represents the constants needed to access various resources of the Android application. More on the relationship between the R class and resources is found below.
- SaySomething.java
- Implementation of the application's primary activity class.
- Referenced libraries
- Contains android.jar, which is the Android runtime class jar file, found in the Android SDK.
- res folder
- Contains the resources for the application, including:
- Icons
- Layout files
- Strings
- AndriodManifest.xml
- Deployment descriptor of the sample application.
Next, we'll examine the source code in further detail.
The primary activity of the application
The sample application consists of a single activity, namely SaySomething. As described above, the SaySomething class is implemented in the file SaySomething.java.
Listing 4. SaySomething.java
package com.msi.ibmtutorial;
import android.app.Activity;
import android.os.Bundle;
public class SaySomething extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
}
}
|
Things to note about this source snippet:
-
SaySomethingis a normal Java class, with a package and imports, as expected. -
SaySomethingextends a base Android class named Activity, which is located in the android.app package. - The
onCreate()method is the entry point to this activity, receiving an argument of typeBundle. TheBundleis a class which is essentially a wrapper around a map or hashmap. Elements required for construction are passed in this parameter. This tutorial does not examine this parameter. - The
setContentView(..)is responsible for creating the primary UI using the R.layout.main argument. This is an identifier representing the main layout found in the resources of the application.
The next section reviews the resources for the sample application.
Resources in Android are organized into a subdirectory of the project named res, as described previously. Resources fall into three primary categories:
- Drawables
- This folder contains graphics files, such as icons and bitmaps
- Layouts
- This folder contains XML files that represent the layouts and views of the application. These will be examined in detail below.
- Values
- This folder contains a file named strings.xml. This is the primary means for string localization for the application.
The next section dissects the main.xml file to review the sample application's primary UI resources.
The sample application contains a single activity and a single view. The application contains a file named main.xml that represents the visual aspects of the primary UI of the activity. Note that there is no reference in the main.xml where the layout is used. This means it may be used in more than one activity, if desired. Listing 5 contains the content of the layout file.
Listing 5. Layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, SaySomething"
/>
</LinearLayout>
|
This is the most simplistic of layouts. There is a single linear layout, which is
oriented as a vertical layout, meaning all contained elements are in a single column.
There is a single TextView element, which can be likened to
a label in other development environments. A TextView
represents static text that is not editable.
Note that each view element (layout and TextView in this example) have attributes in the Android name space.
Some attributes are common to all views — the android:layout_width and android:layout_height attributes, for example. The values available for these attributes are:
- Fill Parent
- This extends the view element to take the maximum space available. This can also be thought of as meaning "stretch."
- Wrap Content
- This value tells Android to paint the elements one after the next without stretching.
During the build process, all resources are compiled. One of the products of that process is the R.java file, which represents the resources to the remainder of the application. The R.java file is discussed next.
The R.java file is created upon build automatically, so be sure to not modify it by hand as all changes will be lost. Listing 6 contains the R.java file for the sample application.
Listing 6. R.java file
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/
package com.msi.ibmtutorial;
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int icon=0x7f020000;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class string {
public static final int app_name=0x7f040000;
}
}
|
The R class contains anonymous subclasses, which each
contain identifiers for the various resources previously described. Note that all of these classes are static.
Note the element represented by: R.layout.main. This identifier represents the layout
defined by main.xml. Recall that this value is used in the onCreate method of the activity as follows: setContentView(R.layout.main);. This is the point at which a specific activity (in this case, SayAnything) and a specific layout (main) are bound together at runtime.
Files are compiled every time they are saved by default.
Figure 5. Error pane
We introduced an error into the source code where we added an extra space between setContent and View. When the file is
saved, it is compiled and any errors appear in the Problems pane at the bottom of the
screen. Upon fixing the error in the source code, the application builds properly and the errors are removed from the problems list.
The AndroidManifest.xml file represents the deployment descriptor for an Android
application. The file lists any activity, service, content provider, or receiver
contained in the application, along with the appropriate IntentFilters supported by the application. Here is the complete
AndroidManifest.xml file for the sample application:
Listing 5. AndroidManifest.xml file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.msi.ibmtutorial">
<application android:icon="@drawable/icon">
<activity class=".SaySomething" android:label="@string/app_name">
<intent-filter>
<action android:value="android.intent.action.MAIN" />
<category android:value="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
|
Things to note:
- The package name from the source file is represented here. This follows a similar
pattern to a Java source file and imports. The
<manifest>tag is in essence "importing" classes from this package. All non-fully qualified classes in this file are found in the package identified in the package attribute. - The
<application>tag has an attribute that references a resource from the application's resources. Note the@symbol preceding the drawable identifier. This is a hint for the file to look in the drawable folder of the application's resources for a resource called "icon." - The
<activity>tag contains the following attributes and values of note:
- class represents the Java class implementing this activity
-
android:labelis the name of the application. Note that it is coming from one of the string resources. The string.xml file contains localized strings for the application. -
<intent-filter>represents theIntentFilteravailable in the sample application. This is the most commonIntentFilterseen in Android applications. This filter essentially says that it implements the "main" action (or entry point) and is located in the launcher of the OS. In English, this means it can be started as an application from the primary list of applications on an Android device.
The next section describes starting the application on the Android Emulator from within Eclipse.
Now that the application has compiled successfully, it's time to run the sample application. Select Open Run Dialog or shortcut on the toolbar within Eclipse. This opens a dialog where startup configurations are created. Highlight the Android Application option and click the icon for New.
Figure 6 show the values used for the tutorial sample.
Figure 6. Run dialog
Give the configuration a name. The tutorial sample uses the name Tutorial Configuration. Select the ibmtutorial project from the list of available projects (click Browse to see available projects). Select the startup activity in the drop-down. Now select the Emulator tab to specify Emulator settings, as desired. The default can be left alone. There are a couple of items to note, as described in Figure 7.
Figure 7. Run dialog, Emulator tab
There are a few screen sizes and orientations to choose from, as well as network choices. The network choices are important when building applications that employ Internet connectivity as mobile devices have varying network speed capabilities. Choose full network speed and no latency when prototyping an application. Once the main functionality is present, it's a good idea to test with less-than-ideal network conditions to see how the application responds in situations with suboptimal network connectivity.
Select Run to see the sample application in action.
Figure 8. Emulator
Now that the application is running on the Emulator, it's time to see what's happening behind the scenes. The Dalvik Debug Monitor Service (DDMS) will assist with this.
To see what is happening with a running application, it is helpful to tap into the running Dalvik VM. To enable this from Eclipse, select Window > Open Perspective > Other. This displays a dialog box where the DDMS may be selected. This opens a new perspective in Eclipse with a number of interesting windows. Here is a quick introduction to the available resources in the DDMS perspective:
The LogCat is a running log file of activity taking place in
the VM. Applications can make their own entries to this list with a simple line of
code as follows: Log.i(tag,message);, where tag and message are both Java strings. The
Log class is part of the android.util.Log package.
Figure 9 shows the LogCat in action.
Figure 9.
LogCat in action
Another handy tool in the DDMS is the file explorer, which permits file system access of the Emulator. Figure 10 shows where the tutorial's sample application is deployed on the Emulator.
Figure 10. Sample application deployed on the Emulator
User applications are deployed in /data/app while Android built-in applications are found in the /system/app directory.
A running process list is also available in the DDMS.
Figure 11. Running process list
Full-scale debugging of an Android application is beyond the scope of this tutorial. For more information, see Resources.

