Java Android Lab Excercises. Updated 25 Sep 2012

nb - Lab book is the recommended text:: "Android Developer's Cookbook" by Steel/To

Exercise 1.0 First Program

  1. Create an anroid program using the steps in getting started
  2. Find the "hello word" text in the XML document and modify

Exercise 1.1 Using Eclipse Grahical Layout Builder (see page 83 Android Developers Cookbook)

  1. Create an anroid program using the steps in getting started
  2. Open main.xml in graphical mode and drag user interface element onto the screen and run the apps to see how they work
  3. Take a new activity and open its layout resource XML file. Here e.g. the main.xml file.
  4. Click the Layout tab.This shows how the layout would look graphically.
  5. Click the user interface screen and remove everything to start from scratch
  6. Click and drag a layout from the Layouts Selector to the screen area. For example,
    choose TableLayout, which holds multiple Views or ViewGroups down a column.
  7. Click and drag any other layouts to nest them inside the first one. For example,
    choose TableRow, which hold multiples Views or ViewGroups along a row.Add
    three of these for this example.
  8. Right-click each TableRow in the Outline view and add view elements from the
    Views Selector. For example, add a Button and CheckBox to the first TableRow, two
    TextViews to the second, and a TimePicker to the third.
  9. Add a Spinner and VideoView view below the TableRow elements.


Exercise 2.0 LifeCycle - Using System.out instead of 'Toast'

  1. Load and run Listing 2.6 src/com/cookbook/activity_lifecycle/ (page 32) (NOT ActivityLifecycle_kg)
  2. Change the Toast messages to System.out.println("....
  3. In Eclipse find the LogCat view - Window | ShowView | Other | Android | LogCat
  4. Run the program and see the LogCat tab.
  5. Looking at the LogCat tab look at the type of messages being shown when you exit and launch your program. It will be difficult to see the System.out message so we can create filter for them - see next step.
  6. Create a filter in LogCat to show only System.out messages: the green + sign then name the filter (eg. system out) then enter a tag of System.out
  7. A new tab is now display with your named filter (eg. system out)
  8. Change emulator orientation etc to see System.out messages in LogCat (doesn't seem to work on cloud/VM setup)
  9. NB Changing the orientation in emulator 2.3.* doesn't work - there's bug. Use Anroid 3.x or 4.x.

Exercise 2.1 LifeCycle - see how long a variable is kept in memory e.g. when is Activity destroyed

  1. Continuing with (page 32)...
  2. In the onCreate( ) method generate a random number using Math.random() method. Save in a variable.
  3. Modify each of the Toast methods to display that random number to see when it changes (indicating the original number was lost)
  4. Run, change orientation, pause, exit, restart etc. the app to see what things cause the original random number to be lost.
  5. Add onRestoreInstanceState( ) & onSaveInstanceState( ) with their own Toast message to see when they run.


Exercise 2.2 LifeCycle - Saving a variable using onRestoreInstanceState( )

  1. Load the modified version of (page 32). it's in the ActivityLifecycle_kg folder. See the new code in the onCreate method and also the 2 methods onRestoreInstanceState and onSaveInstanceState and observer how the variable resumeCnt is saved/lost when you exit the app or switch to a different app or change the Apps orientation (Cntrl -F11).
  2. Again - Run, change orientation, pause, exit, restart etc. the app to seen what things cause resumeCnt value to be lost.
  3. NB Orientation changes don't work with emulator version 2.3.* so use a different one.

Exercise 3.0 Tapping a Button displays a message

  1. Create an app with one activity - the activity has a Button and a TextView.
  2. Declare an int variable.
  3. Tapping on the button increments a counter an it's new value is displayed in the TextView.
  4. See the code example in the book on page 36 - Using Buttons and TextView

Exercise 3.1 One Activity Launches another

  1. Run Listing 2.8 src/com/cookbook/launch_activity/
  2. Add a new button to MenuScreen labelled 'show scores' that launches an 3rd activity - call is 'show scores' which displays some text.

Exercise 4.0 Controlling width & height of UI Widgets

  1. Create an activity with a horzontal linear layout with buttons (see page 86 of the book)
  2. See the impact of changing the following parameters for each button: width, height (wrap & fill parent, exact dimension)
  3. Change the padding (left right etc)
  4. Replicate the screen shots from the book - figs 4.4, 4.5, 4.6

Exercise 4.1 Combining Horizontal & Linear layouts

  1. Using top level Vertical Layout that fills the screen horizontally.
  2. Add 4 Horizontal Layouts - e.g 4 rows
  3. The 1st horizontal layout (row) contains 4 TextView items (columns) right-justified and each taking up the same space. The column headings are:
    1. Expense
    2. 2011
    3. 2012
    4. 2013
  4. Add 3 rows each with TextView containing expenses data e.g. Electrictiy 500 550 900
  5. Make some data values to big e.g. 200,000 or more to see how the layout manages.
  6. Check behaviour with the emulator in horizontal and vertical portrait modes.
  7. Add a 5th row with 1 button on the far right labelled 'Clear'
  8. Pressing the clear button sets the expenses in columns 2, 3, and 4 to '0'. So some java code needed.

Exercise 4.2 Programming Relative Layout

  1. Run example 'programmatic layout' on P91 of book
  2. Add a new TextView in the top right hand corner

Exercise 4.3 Capturing Text from an EditText Object
  1. Create a new project and use code on P101 of book to capture text from user.
  2. Set the entry mode for the EditText to only accept numbers
  3. When the enter key is pressed the activity converts the entered text to an int and adds it to previous numbers added









************* This is old stuff, ignore ********************

Java Mobile Lab Excercises. Updated 05 Oct 2008

Exercise 1.0 First "HelloCIT" midlet

  1. Get the HelloCIT midlet on the "getting started" page working.
  2. Make some changes - e.g. the text of the String item.
  3. Put in an error - e.g. divide by zero, to see how the development environment attempts to point out on the PC when a runtime error occurs on the phone emulator.

Exercise 2.0 First MIDlet

  1. Get the MIDlet "First MIDlet Progam" in the handout working (ok, so it's really our second MIDlet). Copy the code from the handout.
  2. Modify the MIDlet by additing these additional items to the form e.g. TexField, DateField, Gauge. Look up the lcdui package to see what Items can be added and the parameters needed..
  3. You can output to the PC console while the program is running e.g. place this code in the constructor:

    System.out.println("in Constructor"); // This will ouput on the PC console, not on the phone

  4. Now add :System.out.println("in CommandAction method"); to the CommandAction method to see when that method is running.
  5. Add more Sytem.out.println's in the following methods:
  6. Note the sequence of method calls from MIDlet start to end.

Exercise 2.2 First MIDlet - adding a new command

  1. Continue to add to 2.0 First MIDlet by adding an "OK" command (look up the API command class)
  2. Have the"OK" command display on the phone's screen.
  3. Add code to process the "OK" command
  4. In the actionCommand method display the contents of the TextFrield using System.out.println ( )
  5. Add two more commands e.g. Send, Spell Check.
  6. Where were they placed?
  7. Add code to check for these commands - add System.out.println's to show when that code is being executed.
  8. Now use System.out.prinln in the OK processing code ad see the text being modified whil the program runs.
  9. Add another System.out.prinln in the OK to display the value of the gauge (if it's not interactive, go back to the API to see how to make it interactive)

Exercise 3.0 Additon MIDlet

  1. Create a MIDlet that allows you to enter a number. The number is then added to any prevous number and the running total result is displayed. Use a TextBox to recieve text from the user (instead of a Form as in the previous exampe).
  2. Can you crash the program by entering text instead of numbers? If you can then constrain the user input to numbers only.

Exercise 3.1 Additon MIDlet on a real phone

  1. For the addition MIDlet : Use the IDE to Create a JAR file. 
  2. (Optionally) Transfer the JAR file to you phone and test. See handout on how to create and deploy a JAR file.

Exercise 4.0 Battery Status

Create an MIDlet that displays a coloured bar to diplay a car battery's status. The battery voltage is entered into the MIDlet as a floating point number.
Diplay a bar graph as follows:
    0-9.5 - Red  (battery dead)
    >9.6 <12 - Yellow (battery poor)
    >12  <14.4 - Green (battery good)
    >14.4 - Blue (Alternator faulty)

Exercise 5.0 Secret Text

Develop an MIDlet  that has a TextField  and  Label GUI components.
When a piece of text is entered the MIDlet 'encrypts' the text by replacing each letter using the following mapping:

    So A -> Z,  N-> M,  B-> Y,  O->L etc

Display the encrypted text back in the TextField (so pressing enter should give you back the original text).
Display the length of the entered text using the Label.

Exercise 6.0 Missing Letter Game

Develop an MIDlet or application that displays a word at random with a random letter(s) missing. The user has to guess the missing letter(s) by entering it/them into a text field(s). You canuse an array or vector to store some words internally in the program.