To Lecture Notes

IT 372 -- Apr 24, 2024

Review Questions

  1. Name events in the Android Activity Lifecycle. Answer:
    Answer: ON_CREATE --Fires when the activity is first loaded into memory.
    ON_START -- Fires when the activity becomes visible, just before it becomes interactive.
    ON_RESUME -- Fires when the activity becomes interactive.
    ON_PAUSE-- Fires just before the activity moves into the background.
    ON_STOP -- Fires when the app has finished running, but while it is still in memory.
    ON_DESTROY -- Fires immediately before the activity is destroyed (removed from memory).
    The event handler OnSaveInstanceState fires just before the activity is destroyed. It can contain code to save the state of the activity in a Bundle object to use in restoring the state when the activity is recreated.
    Also see the Android Developer website:
        https://developer.android.com/guide/components/activities/activity-lifecycle.
  2. How to you write to the information log in the Logcat Window?
    Answer: you can use System.out.println.  You can also use Log.i (the i means information). You can also use Log.v (verbose), Log.d (debug), Log.w (warning) , Log.e (error).
  3. Correct the errors in this TwoWayTempConverter app:
    activity_main.xml  MainActivity.java  colors.xml  strings.xml
  4. How do you add entries dynamically to a Spinner widget?
    Answer: You set the android:entries attribute to a string-array that is defined in the strings.xml resource file. You can also set the entries at runtime by adding the entries to an arraylist, then set up an ArrayAdapter object that helps load the entries from the arraylist into the Spinner object.
  5. Write an Android app that displays these colors in a Spinner widget:
    {"red", "orange", "yellow", "green",
     "blue", "indigo", "violet"}
    
    Add the Spinner items dynamically in the activity code. (Do you know about the acronym ROYGBIV for remembering the colors of the rainbow?) Also, after a color is selected and the linear layout is clicked, change the background color of an ImageView widget to the color selected in the spinner.
    Answer: Here are the layout and activity files:
         activity_main.xml   MainActivity.java
    This activity file uses Method 3 (anonymous class) to implement the click event handler, when the user clicks on the linear layout. Next are the modified layout and activity files that use Method 4 (lambda function) instead, when the user clicks on the ImageView widget:
         activity_main.xml  MainActivity.java
  6. Create an app that displays the state of a Switch widget when the switch is clicked.  Use the onCheckedChanged event listener instead of onClick.
    Answer: activity_main.xml   MainActivity.java (Method 3).
    Here is the Method 4 activity file:
         MainActivity.java

The StopWatch Example

The SendMessage Example