To Lecture Notes

IT 372 -- Apr 3, 2019

Review Questions

  1. What are some of the Android version names?
    Ans: Cupcake, Donut, Eclair, Froyo, Gingerbread, Honeycomb, Ice Cream Sandwich, Jelly Bean, Kitkat, Lollipop, Marshmellow, Nougat, Oreo, Pie.
  2. What is the build language that Android Studio uses?
    Ans: Gradle
  3. For what are XML files used in Android Studio projects?
    Ans: Resource files, layout files, manifest.
  4. For what is Java used in Android Studio projects?
    Ans: Event handlers.

Android Studio Exercise: Magic8Ball Example

Test your app after each of the steps 4, 5, 7, 9, and 10.

  1. Create a default Android Studio Project that displays the "Hello, World!" string.
  2. Change the layout to LinearLayout by using the LinearLayout template.
  3. Replace the string with one of the Magic 8-Ball predictions.
  4. Make these modifications so that the app gets the string to display from the strings.xml resource file:
    1. Add the string definition to the file res/values/strings.xml:
      <string name="pred1">Signs point to yes.</string>
      
    2. Reference this string in the layout file:
      <TextView 
          android:text="@string/pred1" ... />
      
    3. Add all of the the Magic 8-Ball predictions to the strings.xml resource file. Name them pred1, pred2, etc.
  5. Add a Java class named Prediction containing a static method getPrediction that returns one of the twenty Magic 8-Ball predictions. Include a main method for testing. Use an ArrayList collection to hold the predictions. Add predictions from strings.xml file to the collection like this:
    pred.add(getResources( ).getString(R.string.pred1));
    
  6. Assign an ID to the TextView widgit in its Properties Window. This results in this XML statement in the layout file:
    android:id="@+id/pred"
    
  7. Add lines to the MainActivity.java file to randomly display a Magic 8-Ball prediction in the text view.
    1. Get a reference to the TextView widget like this:
      TextView txtPrediction = (TextView) findViewById(R.id.pred);
      
    2. Use the setText method to change the text in the txtPrediction widgit.
  8. If you close and reopen the app, a new prediction should appear, although there is a small chance that the same random prediction will be displayed again. Just keep trying if you get the same prediction.
  9. Choose a background color for the app:
    1. add it to the res/values/colors.xml resource file.  Name the background color background; choose a light pastel color for the background color.
    2. In the layout file, set the background color for the linear layout like this:
      android:background="@color/background"
      
  10. Set up a click event handler for the linear layout to reload the random prediction:
    1. Create a method in MainActivity.java with this header:
      public void reloadPrediction(View view) {
      
    2. Add an attribute to the linear layout file to connect the event handler reloadPrediction with the linear layout:
       android:onClick="reloadPrediction"
      

FontSizes Example

Crazy8Ball Example

Project 1b.

  1. Look at Project 1b.

InnerClass Example