An Android application

This section presents a simple Android application demonstrating a single Activity, with one View. The Activity collects data, a street address to be specific, and creates an Intent to find this address. The Intent is ultimately dispatched to Google Maps. Figure 1.8 is a screen shot of the application running on the emulator. The name of the application is Where Do You Live.

Figure 1.8 This Android application demonstrates a simple Activity and Intent.

As previously introduced, the AndroidManifest.xml file contains the descriptors for the high-level classes of the application. This application contains a single Activity named AWhereDoYouLive. The application's AndroidManifest.xml file is shown in listing 1.6.

Listing 1.6 AndroidManifest.xml for the Where Do You Live application

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

<manifest xmlns:android="" package="com.msi.manning.unlockingandroid"> <application android:icon="@drawable/icon">

<activity android:name=".AWhereDoYouLive" android:label="@string/ app_name">


<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>

The sole Activity is implemented in the file, presented in listing 1.7.

Listing 1.7 Implementing the Android Activity in package com.msi.manning.unlockingandroid;

// imports omitted for brevity public class AWhereDoYouLive extends Activity { @Override public void onCreate(Bundle icicle) {

super.onCreate(icicle) ; O Set up GUI setContentView (R. layout .main) ; <1-'

final EditText addressfield = (EditText) findViewById( ; final Button button = (Button) findViewById (R. id. launchmap) ; button.setOnClickListener(new Button.OnClickListener() {

public void onClick(View view) { try {

String address = addressfield.getText () .toString() ; <1-

Intent geoIntent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=" + address)); <—

startActivity(geoIntent); <1} catch (Exception e) {

Reference C Edit field

Reference button

Initiate lookup G

Prepare Intent

Get address o

In this example application, the setContentView method O creates the primary UI, which is a layout defined in main.xml in the /res/layout directory. The EditText view collects information, which is in this case an address. The EditText view is a text box or edit box in generic programming parlance. The findViewById method Q connects the resource identified by to an instance of the EditText class.

A Button object is connected to the launchmap UI element, again using the findViewById method G. When this button is clicked, the application obtains the entered address by invoking the getText method of the associated EditText O.

Once the address has been retrieved from the UI, we need to create an Intent to find the entered address. The Intent has a VIEW action, and the data portion represents a geographic search query, as seen in Q.

Finally, the application asks Android to perform the Intent, which ultimately results in the mapping application displaying the chosen address. This is accomplished with a call to the startActivity method ©.

Resources are precompiled into a special class known as the R class, as shown in listing 1.8. The final members of this class represent UI elements. Note that you should never modify the file manually, as it is automatically built every time the underlying resources change.

Listing 1.8 contains the R class, which has UI element identifiers


* This class was automatically generated by the

* aapt tool from the resource data it found. It

package com.msi.manning.unlockingandroid;

public final class R {

public static final class attr { }

public static final class drawable {

public static final int icon=0x7f020000;

public static final class id {

public static final int address=0x7f050000; public static final int launchmap=0x7f050001;

public static final class layout {

public static final int main=0x7f030000;

public static final class string {

public static final int app_name=0x7f040000;

Android resources are covered in greater depth in chapter 3.

The primary screen of this application is defined as a LinearLayout view, as shown in listing 1.9. It is a single layout containing one label, one text entry element, and one button control.

Listing 1.9 Main.xml defines the UI elements for our sample application

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

<LinearLayout xmlns:android="" android:orientation="vertical" android:layout_width="fill_parent"

android:layout_height="fill_parent" >

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Please enter your home address." />

<EditText O ID assignment android :id="@+id/address" <1-1 for EditTeXt android:layout_width="fill_parent" android:layout_height="wrap_content"


<Button C ID assignment android :id="@+id/launchmap" <-1 for Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show Map" />

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"

android:text="Unlocking Android, Chapter 1."


Note the use of the @ symbol in this resource's id attribute O and ©. This causes the appropriate entries to be made in the R class via the automatically generated file. These R class members are used in the calls to findViewById(), as shown previously, to tie the UI elements to an instance of the appropriate class.

A strings file and icon round out the resources in this simple application. The strings.xml for this application is shown in listing 1.10. The strings.xml file is used to localize string content.

Listing 1.10 strings.xml

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

<string name="app_name">Where Do You Live</string> </resources>

This concludes our first Android application.

1.5 Summary

This chapter has introduced the Android platform and briefly touched on market positioning, including what Android is up against as a newcomer to the mobile marketplace. Android is such a new platform that there are sure to be changes and announcements as it matures and more and varied hardware hits the market. New platforms need to be adopted and flexed to identify the strengths and expose the weaknesses so they can be improved. Perhaps the biggest challenge for Android is to navigate the world of the mobile operators and convince them that Android is good for business. Fortunately with Google behind it, Android should have some ability to flex its muscles, and we'll see significant inroads with device manufacturers and carriers alike.

In this chapter we examined the Android stack and discussed its relationship with Linux and Java. With Linux at its core, Android is a formidable platform, especially for the mobile space. While Android development is done in the Java programming language, the runtime is executed in the Dalvik virtual machine, as an alternative to the Java virtual machine from Sun. Regardless of the VM, Java coding skills are an important aspect of Android development. The bigger issue is the degree to which existing Java libraries can be leveraged.

We also examined the Android Intent class. The Intent is what makes Android tick. It is responsible for how events flow and which code handles them, and it provides a mechanism for delivering specific functionality to the platform, enabling third-party developers to deliver innovative solutions and products for Android. The main application classes of Activity, Service, ContentProvider, and BroadcastReceiver were all introduced with a simple code snippet example for each. Each of these application classes interacts with Intents in a slightly different manner, but the core facility of using Intents and using content URIs to access functionality and data combine to create the innovative and flexible Android environment. Intents and their relationship with these application classes are unpacked and unlocked as we progress through this book.

The AndroidManifest.xml descriptor file ties all of the details together for an Android application. It includes all of the information necessary for the application to run, what Intents it can handle, and what permissions the application requires. Throughout this book, the AndroidManifest.xml file will be a familiar companion as new elements are added and explained.

Finally, this chapter provided a taste of Android application development with a very simple example tying a simple UI, an Intent, and Google Maps into one seamless user experience. This is just scratching the surface of what Android can do. The next chapter takes a deeper look into the Android SDK to learn more about what is in the toolbox to assist in Unlocking Android.

0 0

Post a comment