Java Android Lab Excercises. Updated 07 Oct 2011
nb - Lab book is the recommended text:: "Android Developer's Cookbook" by Steel/To
Exercise 1.0 First Program
- Create an anroid program using the steps in getting started
- 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)
- Create an anroid program using the steps in getting started
- Open main.xml in graphical mode and drag user interface element onto the screen and run the apps to see how they work
- Take a new activity and open its layout resource XML file. Here e.g. the main.xml file.
- Click the Layout tab.This shows how the layout would look graphically.
- Click the black screen and remove everything to start from scratch
- 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.
- 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.
- 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.
- Add a Spinner and VideoView view below the TableRow elements.
Exercise 2.0 LifeCycle
- Load and run Listing 2.6 src/com/cookbook/activity_lifecycle/ActivityLifecycle.java
- In the onCreate( ) method generate a random number using Math.random() method.
- Modify each of the Toast methods to display the random number to see when it changes (indicating the original number was lost)
- Run, change orientation, pause, exit, restart etc. the app to seen what things cause the original random number to be lost.
- Add onRestoreInstanceState( ) & onSaveInstanceState( ) with their own Toast message.
Exercise 3.0 System.out
- Use the exmple in exercise 2 to changes the Toast messages to System.out.println("....
- In Eclipse find the LogCat view - Window | ShowView | Other | Android | LogCat
- Run the program and see the LogCat tab.
- 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.
- 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
- A new tab is now display with your named filter (eg. system out)
- Change emulator orientation etc to see System.out messages in LogCat
Exercise 3.0 One Activity Launches another
- Run Listing 2.8 src/com/cookbook/launch_activity/
- 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
- Create an activity with a horzontal linear layout with buttons (see page 86 of the book)
- See the impact of changing the following parameters for each button: width, height (wrap & fill parent, exact dimension)
- Change the padding (left right etc)
- Replicate the screen shots from the book - figs 4.4, 4.5, 4.6
Exercise 4.1 Combining Horizontal & Linear layouts
- Using top level Vertical Layout that fills the screen horizontally.
- Add 4 Horizontal Layouts - e.g 4 rows
- The 1st horizontal layout (row) contains 4 TextView items (columns) right-justified and each taking up the same space. The column headings are:
- Expense
- 2011
- 2012
- 2013
- Add 3 rows each with TextView containing expenses data e.g. Electrictiy 500 550 900
- Make some data values to big e.g. 200,000 or more to see how the layout manages.
- Check behaviour with the emulator in horizontal and vertical portrait modes.
- Add a 5th row with 1 button on the far right labelled 'Clear'
- 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
- Run example 'programmatic layout' on P91 of book
- Add a new TextView in the top right hand corner
Exercise 4.3 Capturing Text from an EditText Object
- Create a new project and use code on P101 of book to capture text from user.
- Set the entry mode for the EditText to only accept numbers
- 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
- Get the HelloCIT midlet on the "getting
started" page working.
- Make some changes - e.g. the text of the String item.
- 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
- Get the MIDlet "First MIDlet Progam" in the handout working (ok,
so it's really our second MIDlet). Copy the code from the handout.
- 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..
- 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
- Now add :System.out.println("in CommandAction
method"); to the CommandAction method to see when that method
is running.
- Add more Sytem.out.println's in the
following methods:
- startApp
- pauseApp
- destroyApp
- Note the sequence of method calls from MIDlet start to end.
Exercise 2.2 First MIDlet - adding a new command
- Continue to add to 2.0 First MIDlet by adding an "OK" command
(look up the API command class)
- Have the"OK" command display on the phone's screen.
- Add code to process the "OK" command
- In the actionCommand method display the contents of the TextFrield using
System.out.println ( )
- Add two more commands e.g. Send, Spell Check.
- Where were they placed?
- Add code to check for these commands - add System.out.println's to show
when that code is being executed.
- Now use System.out.prinln in the OK processing code ad see the text being
modified whil the program runs.
- 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
- 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).
- 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
- For
the addition MIDlet : Use the IDE to Create a JAR file.
- (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:
-
- MLKJIHGFEDCBA
- NOPQRSTUVWXYZ
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.