Go to http://sites.google.com/site/androidappcoursev3/ for the most up-to-date versions of these labs.
Intro
The goal of this lab is to learn the fundamentals of developing Android Applications, from project creation to installation on a physical device. More specifically you should gain the knowledge of how to use basic development tools to support the application development process, as well as the key components of an Android application itself. These labs assume that you know the Java Programming Language. If you are not yet a Java programmer, consider taking Java Essentials for Android by Dr. David Janzen at udemy.com. It will get you ready for completing these labs.
Objectives
Activities
1. Setting Up the Development Environment
1.1 Download/Install the SDK
For in-depth instructions, visit Android Installation Documentation. Otherwise perform the following steps.
1.2 Download/Install the Eclipse Plugin
1.3 Download/Install the SDK Platform Components
At the time of writing this lab there are are eight different versions of the Android Platform available, ranging from 1.1 to 2.2. It is best practice to develop for the oldest platform available that still provides the functionality you need. This way you can be assured that your application will be supported by as many devices as possible. However, you will still want to download newer versions of the platforms so that you can test your applications against these as well. Due to the size of each platform component you will only be required to download and develop on one platform for the whole class. We will target the highest platform that the G1 phones support, Android 1.6 (API 4). Before we can begin developing we must download and install this platform:
We're now ready to develop our application.
2. Create "Hello World" Application
2.1 Create a new Android Project
3. Take a Tour of the Application
The application you've just created is very similar to other java applications you may have created in Eclipse. Look in the Package Explorer side bar. Notice that the Android Development Toolkit(ADT) has generated a number of folders and files for you:
The project creation wizard has written the 'Hello World' application for you already. A string resource containing the display text has been placed into the res\values\strings.xml file. The value is named 'hello'.
The xml UI layout has been added to res\layout\main.xml. While you can use the Android Layout Editor to create your xml layout, you can also code them manually yourself. Lets take a look at this file:
Notice the Top Level node, Linear Layout, which defines the style of layout this file will be using. This 'Linear Layout' is perhaps the most basic, and specifies that UI elements will be laid out in a continuous line. It has three properties: orientation, width, and height.
Notice the Second Level node, TextView, which defines a UI element for displaying text. It has three properties: width, height, and the text to display. Notice that the text property is not set to "Hello World!". Instead it is set to reference the resource value which contains the text we want to display. In this case we are choosing to display the contents of the 'hello' value. We do this by using the '@' symbol, followed by the value type of the resource (which is a 'string'), followed by the name of the value (which is 'hello').
4. Run "Hello World" on the Emulator
4.1 On the Emulator
Before we can run the application, we need to setup an Android Vitual Device(AVD), or emulator, to run it on:
We're now ready to run our application.
Congratulations! You've just created and an Android Application.
4.2 On a Physical Device
Before we can run the application on a physical device we need to modify the project, make a configuration change on the phone, and install some drivers for the phone on our development machine. We begin by making your project declare itself as debuggable. It's possible to do this through the Android Manifest Editor that the ADT provides, however doing this manually helps you understand the manifest better:
Ensure the device is properly connected. Run the application as you would normally. The "Hello World!" app should start on the phone.
5. Simple Activity Classes
There are four major types of component classes in any Android application:
In this lab, we will be focusing on what Activities are and how they are used. We will cover the other components in later labs. If you would like more information on these components, visit the Android overview page for Application Components.
In case you haven't figured it out by now, you have already created one of these component classes. That's right, the HelloWorld class is an Activity Class. It's a simple user interface designed to greet the user. In the section that follows, we'll make our application more personal by adding a new Activity class to ask for the user's name. We'll then update the existing HelloWorld greeting Activity to display that name.
Note: If you ever have problems getting things to compile in Eclipse, you might try Project -> Clean. You can also try to delete R.java under res, then Project -> Build Project.
5.1 Getting the User's Name
To get the user's name, you will be creating an Activity class which will allow the user to enter their name into a text field and press a button when finished to proceed to the HelloWorld greeting Activity. There are three separate steps to accomplish here. You must first layout your user interface in XML. Then you must create the Activity class to parse the input from the user and initiate the HelloWorld Activity. Finally, you will have to reconfigure the application to use your new name retrieval Activity on startup.
5.1.1 Create the User Interface
Android allows you to layout your user interfaces using a simple XML specification. We will go into more depth on this topic in the next lab, so for now you will be setting up a basic interface using four different GUI elements. Begin by creating a new Android XML file:
Each GUI element derives from the View base class. The first element was added for you when you created the XML layout file and selected LinearLayout from the dropdown menu. You should be able to see an XML opening and closing tag labeled LinearLayout in the editor. Each XML layout file must have a single root view tag, inside which all other view tags are nested. The LinearLayout tag tells Android to arrange elements contained inside it in a straight line in the order in which they appear. Let's make a few modifications to the LinearLayout by editing the attributes contained in the opening LinearLayout tag:
Lets add the other three UI elements to our XML layout file:
5.1.2 Create the Activity Class
Using the HelloWorld Class from section 2.1 as an example, create a new class that extends android.app.Activity class and implements android.view.View.OnClickListener interface.
5.1.3 Reconfigure the HelloWorld Application
The Android Manifest contains information on all of the Application's components, including which component should be used in different scenarios. We need to tell our application to use the new activity on startup instead of the HelloWorld Activity which it was previously using. Begin by Double-Clicking the AndroidManifest.xml file to open it
At this point, you should be able to try running the application. The application should start up and display the name retrieval activity you just created. You should be able to enter your name in the text field and hit the button, after which the old HelloWorld greeting activity should appear.
5.2 Greeting the User
Now that we've got the name retrieval activity completed, let's update the HelloWorld greeting Activity to print out the name the user entered. In the OnCreate method of HelloWorld.java:
6. Exporting Your Application
In order for your application to be run on a physical device, it must be digitally signed with a certificate. The certificate does not need to be signed by a certificate authority like Verisign. It's acceptable to use a self-signed certificate. As of now, you've only been executing your application on your physical device by launching it through Eclipse. By doing it this way, the Eclipse ADT Plugin has been signing your application with its own "debug" certificate. Signing with the "debug" certificate is not acceptable for release to the general public. In this short section you will learn how to generate a new keystore, compile your application as a ".apk" file, create a certificate, and sign it. For more details on the Application signing you can view the documentation on the Android Developer Site [click here].
For those of you who haven't taken a Security class or are unfamiliar with the idea of Keys, Certificates and how they work, you can read more information about it on Wikipedia.
The Eclipse ADT plugin provides a simple Export Wizard that automates the entire process for you. Follow the instructions on the Android Developer Site on how to Compile and sign with Eclipse ADT. Make sure to read the short section following this one on Securing Your Private Key. While it's not that necessary for this lab, it will be important when you release an application to the public.
Make sure to keep track of your ".apk" file. You will be handing it in at the end of the lab as proof that you completed the Lab.
7. Deliverables
To complete this lab you will be required to:
Primary Author: James Reed
Adviser: Dr. David Janzen