To Lecture Notes

IT 372 -- Apr 7, 2025

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; save it in the parent folder instead.
  2. Where are the layout and activity files located for an Android app named TextDisplay?
    Answer:
    Full path for activity_main.xml:
        TextDisplay/app/src/main/res/layout/activity_main.xml
    Full path for MainActivity.java:
        TextDisplay/app/src/main/java/it372/ssmith/textdisplay/ActivityMain.java
    Full path for strings.xml:
        TextDisplay/app/src/main/res/values/strings.xml
    Full path for color.xml:
        TextDisplay/app/src/main/res/values/colors.xml
  3.  In this layout file, why does line 13 end with /> but line 7 only ends with >?
     1   <LinearLayout
     2     xmlns:android="http://schemas.android.com/apk/res/android"
     3     xmlns:tools="http://schemas.android.com/tools"
     4     android:layout_width="match_parent"
     5     android:layout_height="match_parent"
     6     android:gravity="center"
     7     tools:context=".MainActivity">
     8
     9     <TextView
    11         android:layout_width="wrap_content"
    12         android:layout_height="wrap_content"
    13         android:text="@string/greeting" />
    14 </LinearLayout>
    
    Answer: the TextView element does not contain any child nodes, so it can be written as a combination start/end node terminated with />. However, the LinearLayout element does contain the TextView node so it cannot be written as a combination start/end tag. The </LinearLayout> tag terminates the linear layout after the TextView tag.
  4. 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.
  5. Create an Android app with a Button widget in a linear layout. Set the text of the button to "Click me.". When the button is clicked, the text should change to "I\'ve been clicked".
    Later in these notes, see the section Attaching an Event Listener using Method 4. Then modify this exercise to use Method 4 instead of Method 1.
    Ans: activity_main.xml  MainActivity.java  strings.xml
    MainActivity class using Method 4: MainActivity.java
  6. Change this 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";
    }
    // Display the output variable contents
    // in a TextView widget:
    tv.setText(String.valueOf(output));
    
    Then display the output in a TextView widget in an Android app.
    Answer: activity_main.xml   MainActivity.java   TempDescriptor.java
  7. 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);
    
  8. 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 one language, switching languages requires only changing to the resource file for that language.
  9. How is a string defined in the strings.xml resource file? Answer:
    <string name="greeting">Hello, World!</string>
    
  10. 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);
    
  11. 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);
    
  12. In the statement
    int count = Integer.parseInt(tv.getText( ).toString( ));
    
    why do we need the the method call toString( )?
    Answer: tv.getText( ) returns an object of type CharSequence, an interface that is implemented by these classes: CharBuffer, Editable, Spannable, Segment, String, StringBuffer, StringBuilder. The actual object returned from tv.getText might not be a String object. This allows for more flexibility than simply returning a String object.
  13. -->
  14. For what is the measurement unit dp used?
    Answer: dp means device independent pixels.
    1 dp = 1/160 in = (1/160)in * 2.54 (cm/in) * 10 (mm/cm) = 0.15875 mm.-->
  15. For what is the measurement unit sp used?
    Answer: It means scale independent pixels. It is also 1/160 in, but it measures the font size.
  16. Create an app named TriColor that consists of a linear layout with blue background and no wigits. Set up an event handler, so that each time the linear layout is clicked, the background color changes blue to red, red to lime, or lime to blue.  Use an int instance variable in the MainActivity class to keep track of the background color. Use the codes Blue=0, Red=1, and Lime=2. Modify your app to use Method 4.
    Ans: activity_main.xml  MainActivity.java  colors.xml
    MainActivity class using Method 4: MainActivity.java

Attaching an Event Listener using Method 4

Margin and Padding Attributes

Project 1b