To Lecture Notes

IT 372 -- Apr 8, 2024

Review Questions

  1. How do you use Android Studio to create a zipfile of your app for submitting on D2L?
    Answer: Select Main Menu File >> Export >> Export to Zip File.
            Don't save the zip file inside the app folder.
  2. What is Hungarian notation?
    Answer: It is when the name of a widget (Android name for a control) has a 3 or 4 letter prefix that indicates the widget type, for example txt for TextView, btn for button, spnr for Spinner. Often hungarian notation is used for Java variable names, or the ids if widgets in the layout file. Hungarian notation was advocated by Charles Simonyi, who was the chief software architect at Microsoft in the early days.
  3. Write an Android app named DisplayItem, using a linear layout, that initially displays the string "Click to see notable item." When the linear layout is clicked, show your notable item. Use an onClick event handler named showItem for the linear layout. Follow these steps:
    1. Add the id attribute to the TextView widget:
      android:id="@+id/txtNotableItem"
      
    2. Add an onClick attribute that designates the name of the event handler showItem.
    3. Add the event handler showItem to MainActivity.java that looks like this:
      public void showItem(View view) {  
          TextView tv = findViewById(R.id.txtNotableItem);
          tv.setText("Sample Notable Item.");
      }
      
      Answer: Source code files: activity_main.xml  MainActivity.java
  4. Change the TempDesc class so that it has the instance variable temperature, the getter getTemperature, the setter setTemperature, and the method getDescriptor, Also include a parameterized constructor that initializes the instance variable. Use this UML diagram:
    +--------------------------------+
    |        TempDesc                |
    +--------------------------------+
    | - temperature : double         |
    +--------------------------------+
    | + TempDesc(temp: double)       |
    | + getTemperature( ) : double   |
    | + setTemperature(temp : double)|
    | + getDescriptor( )             |
    +--------------------------------+
    
    Test your TempDesc class like this:
    double[ ] temps = 
        {-13.0, 27.0, 52.0, 68.0, 87.0, 103.0};
    TempDesc tempObj = new TempDesc(0);
    String output = "";
    for(double temp : temps) {
        tempObj.setTemperature(temp);
        output += tempObj.getDescriptor( ) + "\n";
    }
    System.out.println(output);
    
    Then display the output in a TextView widget in an Android app.
    Answer: Create an Android project named TempDesc. Then replace these files in your app:
         activity_main.xml   MainActivity.java   TempDescriptor.java
  5. Use an object from the Random class to output a random integer between 0 and n - 1? Answer:
    import java.util.Random;
    ---------------------------------
    Random r = new Random( );
    int randomValue = r.nextInt(n);
    System.out.println(n);
    
  6. What is the package of the TextView class? What is its base class? Look this up in the documentation on the Android Developer site:
         developer.android.com
  7. Why should literal strings not be displayed directly on an app layout? How do you move the literal string definitions to the resource file strings.xml?
    Answer: (a) If a string is used more than once on a form, if it is changed, it need only changed once in the resource file.
    (b) For globalization: if an app is available in more than once language, switching languages, requires only switching to the resource file for that language.
  8. How is a string defined in the strings.xml resource file? Answer:
    <string name="greeting">Hello, World!</string>
    
  9. How is a string accessed from a resource file? Answer:
    In a layout file:
    android:text="@string/greeting"
    
    In a Java activity file:
    String s = getString(R.string.greeting);
    
  10. In an event handler, how can you access and change the text that is displayed in a TextView control?. Answer:
    // Access the text:
    String s = txtValue.getText( ).toString( );
    
    // Change the text:
    txtValue.setText(s);
    
  11. In the statement
    int count = Integer.parseInt(tv.getText( ).toString( ));
    
    why do we need the the method call toString( )?
    Answer: tv.getTest( ) returns an object of type CharSequence, which is an interface that is implemented by these classes: CharBuffer, Segment, String, StringBuffer, StringBuilder.
  12. What is the measurement unit dp?
    Answer: dp means device independent pixels.
    1 dp = 1/160 in = (1/160)in * 2.54 (cm/in) * 10 (mm/cm) = 0.15875 mm.
  13. What is the measurement unit sp?
    Answer: It means scale independent pixels. It is also 1/160 in, but it measures the font size.
  14. Create an app named ChangeBackground that consists of a linear layout with pastel blue background and no wigits. Set up an event handler, so that when the linear layout is clicked, the background changes to black.
    Answer: Source code files: activity_main.xml  MainActivity.java

Margin and Padding Attributes

Practice Problem

  1. Implement an Android app named ClickMe with a Button widget having text "Click Me". When the button is clicked, the button text changes to "I've been clicked." Here are screen shot A (before click) and screen shot B (after click).
    Answer:  activity_main.xml   MainActivity.java

Project 1b

Magic8Ball Example