To Lecture Notes

IT 372 -- Apr 6, 2026

Review Questions

  1. What XML attributes do you know for the LinearLayout and TextView views? Answer:
    background  layout_width  layout_height  text  textColor  
    textSize  orientation  gravity  layout_margin  
    layout_marginLeft  layout_marginTop  layout_marginBottom
    layout_marginRight  id
    
  2. How does an Android app know which XML layout file to use when starting the app?
    Answer: the statement
    setContentView(R.layout.activity_main);
    
    which is Line 6 of the onCreate method of MainActivity.java.
  3. Where are the layout, activity, and resource 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
  4.  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.
  5. 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.
  6. 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);
    
  7. 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."
    Source Code Files:
    strings.xml  colors.xml  activity_main.xml   MainActivity.java
  8. 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.
    Answer: Use an int global instance variable in the MainActivity class to keep track of the background color. Use the codes Red=0, Green=1, and Blue=2.
    Source Code Files:
    colors.xml   activity_main.xml  MainActivity.java

Magic8Ball Example

Project 1b

Practice Exercises

  1. 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 be 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.
  2. How is a string defined in the strings.xml resource file? Answer:
    <string name="greeting">Hello, World!</string>
    
  3. 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);
    
  4. 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);
    
  5. 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.
  6. What are the measurement units dp and sp?
    Answer: dp means scale independent pixels, which is defined as 1/160 in = 0.159 mm, sp is the same length, but it measures the font size.