To Lecture Notes

IT 372 -- Apr 21, 2025

Review Questions

  1. The following code copies the items from an arraylist declared by
    ArrayList<Integer> al = new ArrayList<Integer>( );
    
    into a string:
    String output = "";
    for(Integer n : al) {
        output += n + "\n";
    }
    
    Perform this operation using a StringBuilder collection instead. If there are many items in the arraylist, using a StringBuilder collection is much faster.
  2. Suppose you have a HashMap collection:
    HashMap<String, Integer> map = 
        new HashMap<String, Integer>( );
    
    How do you add an Integer object to the collection using the key "K123"? How do you retrieve the Integer object? We will use a similar concept when we store information from an activity in a storedInstanceState object.
  3. Add these strings to an ArrayList collection:
    c  java  kotlin  python  
    
    Then use the ArrayList method replaceAll that accepts a lambda function to convert each of the strings to uppercase.
  4. What is an Android drop-down menu called? How do you set one up?
  5. Write an app that shows a toast when a LongTouch event occurs in a linear layout. See The Android Toast section in the April 16 Notes.
  6. Create an app named TestWidgets. Switch the layout to LinearLayout, with vertical orientation. Add a ScrollView that includes the linear layout. Then place each of these widgets in the linear layout:
    Switch  ToggleButton  Button
    
    When the button is clicked, show the states of the switch and the toggle button.
  7. Create an Android app with four radio buttons with text Freshman, Sophomore, Junior, Senior. Place them in a radio button group. Add a button with text Show Year that shows the selected year using the codes 1, 2, 3, and 4.
  8. How do you restrict the inputs to an EditText widget? For example, how do you set it up so that only numbers can be entered?
  9. How do you display special characters in a widget?
    Answer: You can't use HTML entities, but you can use Unicode characters. See the website unicode.org for code charts. For example, the unicode codes for the greek letters alpha (α), beta (β), gamma (γ) are 0BC1, 0BC2, and 0BC3. Here is an attribute that displays these Greek letters in a TextView widget:
    android:text="Greek letters: \u0BC1, \u0BC2, \u0BC3"
  10. How to you write to the information log in the Logcat Window?
  11. Correct the errors in this TwoWayTempConverter app:
    activity_main.xml  MainActivity.java  colors.xml  strings.xml
  12. 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

The Activity Lifecycle

Some Examples

Projects 3a and 3b

Android Platform