Intro
The goal of this lab is to learn the fundamentals of developing Android applications, including programming tool installation, project creation and coding, and running an app on a physical or virtual Android 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 familiar with Java, consider taking Java Essentials for Android, a Udemy course by Dr. David Janzen. It will prepare you for completing these labs.
Remember to have fun and be excited to learn Android in a world full of mobile development opportunity! Now let's get started.
Objectives
Contents
1. Setting Up the Development Environment
1.2 Install the Android SDK & Eclipse ADT Plugin
1.3 Install Android Platforms and Packages
2. Create "Hello World" Application
2.1 Create a new Android Project
3. Take a Tour of the Application
6.3 Reconfigure the HelloWorld Application
Activities
1. Setting Up the Development Environment
NOTE: The following assumes you have Java already installed on your computer. You need the 32-bit Java Development Kit (JDK) since Android is exclusively 32-bit. More information about installing Java for Eclipse usage can be found here. Eclipse will not run unless a JDK is set up.
NOTE: You will also need to make sure that the Java Path variables are set properly, more information on this can be found here. There is possibly nothing to be done on your end to resolve this, as it may be done automatically for you.
Your first steps towards Android development involve several downloads and installations. Google suggests that you install the Android Bundle (contains all components in one package) found here, but in order to become more familiar with the development components and the environment, this lab will walk you through installing them separately. The first tool you need is Eclipse. Google announced Android Studio at Google I/O 2013. Android Studio is an alternative to Eclipse that looks very promising, but as of this writing, Android Studio is still in early access preview because it is missing some important features.
You can launch Eclipse and make sure it loads properly, then continue on to the next step.
The Android Software Development Kit (SDK) provides everything to meet your Android needs.
Once done, open a command line or terminal and type adb then press Enter. If you receive a very long list of functions and properties, this means that the SDK is set up properly.
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, especially the newest version. One way to check platform version usage is to check Google's Platform Versions page. Google collects data about which platform versions of the Android operating system have accessed Google's web and mobile store, Google Play.
We will target the highest platform that the majority of devices support at the time while still maintaining extensive feature support: Android 2.3.3 (API 10). Before we can begin developing we must download and install this platform.
Follow the walkthrough for launching the SDK Manager and installing platforms and packages.
You're now ready to start developing!
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. Let's explore them a bit more in detail. Feel free to open any files to see what they look like by default.
NOTE: The files and folders with asterisks '*' next to their names are more important for you to look at more closely.
Whew, that's a lot! Now let's explore the app itself a bit more in the context of Android development.
The project creation wizard has written the 'Hello World' application for you already, although it is not close to looking like our final product. A string resource containing the "Hello world!" display text has been placed into the res/values/strings.xml file. The value is named 'hello_world'.
The XML UI layout has been added to res/layout/layout_helloworld.xml. While you can use the Android Layout Editor to create and edit your XML layout, you can also code manually yourself. Lets take a look at this file:
Notice the top level element, RelativeLayout, which defines the style of layout this file will be using. This 'RelativeLayout' is one of the most basic layouts (next to LinearLayout), and specifies that UI elements will be laid out in accordance with other elements inside of the layout. It has quite a few properties, but you want to mainly focus on the width and height as you'll be using those very often. These properties will be discussed in detail later in the lab.
What are the xmlns:android and xmlns:tools properties?
These are namespace declarations in XML. They help distinguish between different versions of identical elements in XML. Most of the time we will not have to worry about namespace clashes, just be aware of their existence. Some Android XML components will require an xmlns namespace declaration.
Now look at the second level element, TextView, which defines a UI element for displaying text. It has a few properties as well: width, height and the text to display. Not all properties are required when defining each Android element.
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. You can read more about AVDs here and about managing AVDs here. This is a good option if you do not own a physical Android device or own an Android device that does not run at least Android 2.3.3, Gingerbread.
You can create AVDs using one of the following steps:
We're now ready to run our application.
How the default application appears on an AVD. This is run on a Nexus S emulator on Fedora.
Congratulations, you've just created and launched an Android Application on an AVD! Now if you have a physical Android device such as an Android phone, we'll run it on the real thing.
4.2 On a Physical Device
Before we can run the application on a physical device, we need to make a configuration change on the phone and install some drivers for the phone on our development machine. Thankfully, the newer ADT plugins have automatically handled debugging, and thus turning on debug mode manually in the AndroidManifest.xml file is not needed! However, we still need to prepare the device (phone) for application installation.
The "Hello World!" app should start on the phone or tablet (click image for full size):
How the default application appears on a physical device. This is run on a Nexus 7 tablet.
There are four major types of component classes in any Android application. Consider them all "entry points" into your application, or ways to have the operating system invoke your Android application.
You can read more about Application Components here.
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.
In case you don't remember it from earlier, you have already created one of these component classes: the HelloWorldActivity class is an Activity Class (although it was probably obvious based on the name). It's a simple user interface designed to greet the user. You can't get much simpler than that!
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, and then update the existing HelloWorldActivity to display that name. It's coding time!
Note: If you ever have problems getting things to compile in Eclipse, you might try Project -> Clean.... You can also restart Eclipse and see if the errors go away.
6.1 Create the User Interface
Android allows you to lay out 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 or components. Begin by creating a new Android XML file:
Making a new XML layout file.
Let's take a closer look at this file now. You should be able to see an XML opening and closing tag labeled LinearLayout in the editor. LinearLayout, like all screen components, derives from the View base class (read a description of the View class here). Each XML layout file must have a single root view component, inside which all other view components are nested. LinearLayout tells Android to arrange elements contained inside itself 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:
What does the "@ /" syntax mean?
In Android development, this is the way to reference resources. For example, look at the android:id attribute in the TextView. This attribute specifies a variable name for referencing this element from within the code. Some important information about this kind of syntax:
1. References are specified with the syntax of @type/handle. For example, the String we just created, name_prompt, is specified like this: @string/name_prompt.
2. IDs are declared with the syntax of @+id/MyId01. They are created with the '+' character, not specified, since they do not exist elsewhere like other resources such as Strings. IDs serve as the identification of a component, and you will use them when we modify the Java files.
3. The text after the forward slash (such as MyId01) is the name, or handle, used to identify the element from within your application code via the Static R class. If you look at R.java now, under the final class String you will now see the handle name_prompt listed there. The text that name_prompt refers to is the String we created in strings.xml in the previous step (What is your name? or similar text you entered).
Great, the XML layout is all set up! Let's see our layout in graphical form now:
Much more intuitive.
Now this is where things get more Java-oriented. It's time to integrate the XML layout into the Java Activity class that we want to use. First we need to create
6.2 Create the Activity Class
Using the HelloWorldActivity Class from section 2.1 as an example, use the Wizard to create a new Android Activity called EnterNameActivity that extends android.app.Activity and implements android.view.View.OnClickListener interface:
Notice that the title of this Activity is also set to Hello World. Since we intend to have our new Activity show up upon launch, the Title property also represents the application's name.
If you click Next before Finish, you will see the list of changes that will be performed on all files in the project upon the creation of this new class. In particular, observe the change to AndroidManifest.xml. We will be touching this file later on to make some manual changes in relation to this change.
Notice that the menu XML files in the res/menu folder are named enter_name.xml and hello_world.xml. You will change their names so they are more easily identified.
6.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 HelloWorldActivity 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 HelloWorldActivity greeting screen should appear (click image for full view):
Also make sure that pressing Back erases the entered name in the EditText field of EnterNameActivity.
6.4 Greeting the User
Now that we've got the name retrieval activity completed, let's update HelloWorldActivity to print out the name the user entered. In the onCreate() method of HelloWorldActivity.java:
The final application in motion. Congratulations!
For those 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. Signing your application is not essential for the remainder of these labs, but it will be important if and when you release an application to the public in the Google Play Store in the future.
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 it.
Primary Authors: James Reed, Kennedy Owen
Adviser: Dr. David Janzen