Mobile App Tutorial Deploying A Handler and Runnable For Timed Events Creating A Counter Description
Mobile App Tutorial Deploying A Handler and Runnable For Timed Events Creating A Counter Description
External Function Call initiates the Sequence (from onCreate() or a user interaction).
Desired Action takes place via a function call.
A Check condition is measured within the function call.
Depending on Check condition, a Runnable is called with a Delay.
Runnable object Calls the desired Function and Sequence again.
When Check Condition is False exit the sequence.
Process:
1. Open the activity_countdown.xml file:
2. Modify the XML Code to setup the textTimer TextView object and the resetButton Button
Object. You will also need to change the layout to Linear Layout.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Countdown" >
<TextView
android:id="@+id/textTimer"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/resetButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="reset"
android:text="Reset" />
</LinearLayout>
2.
Modify the onCreate() method to bind the user interface objects to XML and initialize the
variables and Handler. Note that on line 37, the count function is called by onCreate() to start
the Handler / Runnable Sequence.
3. Write the function for reset. This is called by the restButtons onClick attribute.
4. Write the count() function to increase the count, display, and call the Runnable.
6. Save and run the program on the emulator or device. The App should count up in seconds.
(Yes, I know it is not that exciting . . . More will come later).
Classwork Scoring: (Maximum 25 Points)
This App is misnamed! It does not count down, it counts up. Please note the scoring for options to fix
the App.
1. Complete the Directions and create a working CountDown App as described above: (20 Points)
2. Modify the App to Count Down from 60 and stop at 0. (2 points)
3. Modify the App to Include a Button to Pause and Restart the Count. (3 points)