Go to http://sites.google.com/site/androidappcoursev3/ for the most up-to-date versions of these labs.
In this lab we will be learning how to use and extend the Android user interface library. In a number of ways it is very similar to the Java Swing library, and in perhaps just as many ways it is different. While being familiar with Swing may help in some situations, it is not necessary. It is important to note that this lab is meant to be done in order, from start to finish. Each activity builds on the previous one, so skipping over earlier activities in the lab may cause you to miss an important lesson that you should be using in later activities.
Objectives
At the end of this lab you will be expected to know:
Activities
For this lab we will be creating a "Joke List" application. It is a simple app that allows a user to view and edit a list of jokes. All tasks for this lab will be based off of this application. Over the course of the lab you will be iteratively refining and adding functionality to the Joke List app. With each iteration you will be either improving upon the previous iteration's functionality, or you will be implementing the same functionality in a different way.
IMPORTANT:
You will be given a Skeleton Project to work with. This project contains all of the java and resource files you will need to complete the lab. Some method stubs, member variables, and resource values and ids have been added as well. It is important that you not change the names of these methods, variables, and resource values and ids. These are given to you because there are unit tests included in this project as well that depend on these items being declared exactly as they are. These unit test will be used to evaluate the correctness of your lab. You have complete access to these test cases during development, which gives you the ability to run these tests yourself.
1. Setting Up...
1.1 Creating the Project
To begin, you will need to download and extract the skeleton project for the JokeList application.
Next you will need to setup a "Joke List" Android project for this app. Since the skeleton project was created in Eclipse, the easiest thing is to import this project into Eclipse.
1.2 Fill in the Joke Class
Throughout the lab you will be working with the Joke object class. This class will be used to encapsulate two items pertaining to jokes.
Begin by filling in this class:
1.3 Retrieving the Joke Resources Strings
The skeleton project has been pre-populated with an array of three different String resources you can use as sample jokes. For a complete background on Resources and how to properly use them you can see the Android Developer Guide on Resources. For an overview:
You need to display these jokes when the application starts up. When an Activity first starts up, its "public void onCreate(Bundle savedInstance)" method is always called. This method allows you to initialize the Activity. Right now you will only be initializing local variables to hold the jokes that you just entered:
When creating a new Activity you will almost always override the onCreate method. The onCreate method, as you will see later, is the place where you will be creating your User Interfaces, binding data to different UI controls, and starting helper threads (this will not be covered in this lab). Also, take note of the "Bundle savedInstance" parameter that gets passed in. This variable contains information on the previous state of the UI. As you may have assumed, this means that you are able to save the state of the UI before it closes so that it can be initialized to the same state the next time it is created.
2. Brief Background on View Classes
In Android, a user interface is a hierarchy composed of different View objects. The View class serves as the base class for all graphical elements, of which there are two main types:
Layouts are all subclasses of the ViewGroup class and their main purpose is to control the position of all the child views they contain. Layouts can be nested within other layouts as well, to create complex user interfaces. Some of the common layout objects you will use are:
Declaring the layouts for your user interface can be done dynamically (in code), statically (via an XML resource file), or any combination of the two. In the following subsection you will build a set of user interfaces in code. In a future lab, you will build a set of user interfaces in XML.
3. SimpleJokeList
Work done in this section will be limited to the SimpleJokeList.java file. SimpleJokeList is an Activity class which displays a vertical scrollable list of all the Jokes in m_arrJokeList. Additionally, it has the ability to add jokes to m_arrJokeList via a text field and add a button that floats at the top of the screen.
3.1 Declaring Dynamic Layouts in Code
If you've ever built UIs in Java using Swing, this should be somewhat familiar. You generally want to try to define the layouts for your interface using XML when possible, but there are plenty of instances where it is still necessary to do it at run-time in code. For instance, you may want to dynamically change the layout based on some type of user input. Never the less, working with the actual code is good practice for understanding how the different Layout classes work and what sorts of interfaces all the various controls offer.
3.1.1 Display a Scrollable Vertical List
Your first Task is to simply display each of the jokes in the strings.xml resource file in a scrollable vertical list. When finished your application should look something like this:
<color name="dark">#3D3D3D</color>
<color name="light">#1F1F1F</color>
3.1.2 Adding Your Own Jokes
In this next task we will give the user the ability to enter their own jokes by adding a text field and a button to the UI. The text field and button should float at the top of the screen, above the list of jokes. When you scroll through the jokes, the text field and button should not scroll, but remain at the top of the screen.
In order to accomplish this we will walk through a process of nesting LinearLayouts within each other to achieve the desired effect. However, feel free to experiment and use whatever type of ViewGroup Layout you want, so long as the functionality and appearance remains the same. In the figure shown below, you can see how a root vertically-oriented-LinearLayout containing a horizontally-oriented-LinearLayout and the ScrollView of jokes can be combined to achieve the effect we want.
In this step you will only be updating your Layout. The new "Add Joke" button and text field won't work yet. You will add code to hook-up those controls in section 3.2 Simple Event Listeners.
3.2 Simple UI Event Listeners
Now that your UI has the components necessary to allow users to enter new jokes, you need to hook these components up. In this section you will define and register event listeners to handle adding new jokes.
Read the Android Developers Guide on Handling UI Events to get a detailed background on event handlers and listeners. In short, you use event listeners to respond to user interaction with your UI. There are two general steps for doing this. The first is to define an object that implements an interface for the type of interaction you want to respond. The second step is to register that object with the UI control you want to respond to. For example, to react to a particular Button being clicked, you have to register an object that implements the OnClickListener interface with the Button you want to listen to.
3.2.1 Adding a Button Listener
Setup the onClickListener for the "Add Joke" Button. When the user hits the "Add Joke" Button a new Joke object should be initialized with the text from the EditText field and added to your joke list. If the EditText is empty, then the Button should do nothing. In initAddJokeListeners():
m_vwJokeButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
//Implement code to add a new joke here...
}
});
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(m_vwJokeEditText.getWindowToken(), 0);
3.2.2 Adding Key Listeners
Your next task is to add event listeners that respond to certain keys being pressed on your own. When the user is entering their joke into the EditText and presses either the "Enter/Return" key or the "DPad-Center/Track-Ball" key, the application should respond exactly the same as it does when the "Add Joke" Button is pressed.
Add this code to the initAddJokeListeners() method. Remember that after the joke gets added, the soft-keyboard should disappear.
Hints:
3.2.3 Run SimpleJokeList Tests
Primary Author: James Reed
Adviser: Dr. David Janzen