Introduction to Android 2 application development
Written by Sing Li   
Wednesday, 25 August 2010
Article Index
Introduction to Android 2 application development
iProgrammer example
A practical Activity
Ready to run
Using the Eclipse ADT


Connecting Java Code to the Components in the XML UI Descriptor

In the i-Programmer App, when the button is clicked, a big "Thank you!" is printed in the TextView.

The code responsible for handling the button click is in the IProgrammerFirstAndroidActivity class. In fact, it is an anonymous OnClickListener installed by calling the Button's setOnClickListener() method. Review this code in Listing 1.

To find the button component (also a View) in the Java code, you can use the id of the component specified in the main.xml file. The button component can be located via its id by calling the findViewbyId() method. The R class is actually Java code that is generated from the res/layout/main.xml UI description. The generated class provide easy access to UI resources via its configured XML ids. For example, findViewById( locates the element with ID "button" in the res/layout/main.xml file. If you take a look at the generated R class, you can see how is actually a resource id 0x7f050001, see Listing 4.

Listing 4

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 button=0x7f050001;
public static final int text=0x7f050000;
public static final class layout {
public static final int main=0x7f030000;
public static final class string {
public static final int app_name=0x7f040000;
public static final int thankyou=0x7f040002;
public static final
int voteiprogrammer=0x7f040001;

Running the Android App on Your Phone

Now that you know how the application is constructed and have it running perfectly on the emulator, you are ready for deployment to the actual phone.

Android is quite possibly the easiest smart phone platform to deploy applications to - requiring very few steps before you can see the results.

First and foremost, your application must be in a signed package. The good news is that the Eclipse ADT environment, when compiling your code in debug mode, has automatically done the packaging and signing for you. In fact, it even has run zipalign against the binary - an optimization tool that makes the binary package more efficient to load on the actual phone.

The signed package is located under the bin directory of your Eclipse project, and has an .apk (Android Package) extension.

For the sample project, it will be named iProgrammer Android.apk.

Next, you must relax the security setting of the phone to allow installation of applications from an unknown source (not from the Android Market). This is typically done by selecting menu, then Settings, and then Applications. See Figure 8.


Figure 8


The next step is to move the APK file from your computer to the storage on the phone.

This is typically done via the USB cable supplied with your phone. Use this cable to connect the phone to your computer. On my phone, the default mode is a read-only drive; I need to touch a notification to switch the phone to storage mode.This behavior may be different with your phone.

Once your phone appears as a writable drive to the computer, just drag and drop the APK file from the PC to the phone.

Disconnect the cable, then use the Android File Manager App to locate the APK file. Click on the APK file and it will install.

You can now try the i-Programmer App on your phone.








Last Updated ( Sunday, 19 September 2010 )