To Lecture Notes

IT 372 -- Apr 3, 2024

Review Exercises

  1. What are some Android API version names?
    Answer: Here is a list of these version names, named after deserts or sweets:
          apilevels.com
  2. For what are XML files used in Android Studio projects?
    Ans: Resource files and layout files. They are also used for the Manifest, which we will look at later.
  3. For what is Java used in Android Studio projects?
    Ans: Event handlers, which are methods that are executed by the operating system when the event to which the method is connected occurs. Some common events are Create, Click, LongClick, FocusChange, Destroy.
  4. Java Question: what is the difference between a static method and an instance method?
    Ans: A static method is called from a class; an instance method is called from an object. For example, the main method in a traditional Java class is always static because it is called initially before any objects have been created. The String format method is static:
    String s = String.format("Name:%s; Age:%d", name, age); 
    
    The String toUpperCase method is an instance method:
    String t = s.toUpperCase( );
    
  5. How does the toString method work for a class? What does @Override do?
    Ans: The toString method for a class returns a String representation of an object. @Override means that the method is overriding a method in the base class of the current class.
  6. Create an array that contains these strings:
    ibm  oracle  microsoft  google
    
    Print the array using a modern for loop.
     
    // Answer:
    String[ ] names = {"ibm", "oracle", "microsoft", "google"};
    for(String names : name) {
        System.out.println(name);
    }
    
  7. Create an ArrayList collection that contains the same strings as the array in Problem 6. Then print the arraylist. Ans:
    ArrayList<String> names2 = new ArrayList<String>( );
    for(String name : names) {
        names2.add(t);
    }
    System.out.println(names2);
    
  8. Recreate the TextDisplay app from April 1; call your app Prob9. Change the layout to LinearLayout and set the text in the TextView widget to "Hello, my name is <your name>" :
    android:text="Hello, my name is Stan."
    
    Then make these changes to the app:
    1. Change the title of the TextDisplay Example from "Prob9" to "TextDisplay Example":
      In the file app/src/main/res/values/strings.xml, change the app_name like this:
      <string name="app_name">TextDisplay Example</string>
      
    2. What does the XML attribute android:gravity mean in the layout file? Answer:
      android:gravity="center"
      
      means that items in the layout will be centered vertically and horizontally.
      android:gravity="center_horizontal"
      
      means center horizontally but not vertically.
      android:gravity="center_vertical"
      
      means center vertically but not horizontally.
    3. What do the XML attribute values match_parent and wrap_content do? Answer:
      android:layout_width:match_parent
      android:layout_height:match_parent
      
      means that the width and height of the item will expand as much as possible inside the parent layout, which for our example is the linear layout.
      android:layout_width:wrap_content
      android:layout_height:wrap_content
      
      means that the width and height of the item will shink to the height and width of its contents.
    4. Change the background color of the layout (android:background) to yellow. Answer:
      android:background="#FFFF00"
      
      sets the background color to yellow.
    5. Change the foreground color (android:textColor) TextView widget to teal (dark bluegreen). Answer:
      android:textColor="#008080

Android Studio Folder Structure

XML Primer

Device Independent Pixels

Some LinearLayout and TextView Attributes

Setting up a Click Event Handler

Projects 1a and 1b

  1. Look again at Project 1a and Project 1b.