To Lecture Notes
IT 372 -- Apr 3, 2019
Review Questions
- 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.
- What is the build language that Android Studio uses?
Ans: Gradle
- For what are XML files used in Android Studio projects?
Ans: Resource files, layout files, manifest.
- 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.
- Create a default Android Studio Project that displays the "Hello, World!" string.
- Change the layout to LinearLayout by using the
LinearLayout template.
- Replace the string with one of the
Magic 8-Ball predictions.
- Make these modifications so that the app gets the string to display from the
strings.xml resource file:
- Add the string definition to the file res/values/strings.xml:
<string name="pred1">Signs point to yes.</string>
- Reference this string in the layout file:
<TextView
android:text="@string/pred1" ... />
- Add all of the the Magic 8-Ball predictions to the strings.xml resource file.
Name them pred1, pred2,
etc.
- 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));
- 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"
- Add lines to the MainActivity.java file to randomly display a Magic 8-Ball prediction
in the text view.
- Get a reference to the TextView widget like this:
TextView txtPrediction = (TextView) findViewById(R.id.pred);
- Use the setText method to change the text in the
txtPrediction widgit.
- 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.
- Choose a background color for the app:
- add it to the
res/values/colors.xml resource file. Name the
background color background; choose a light pastel color
for the background color.
- In the layout file, set the background color for the linear layout like
this:
android:background="@color/background"
- Set up a click event handler for the linear layout to reload the random prediction:
- Create a method in MainActivity.java with this header:
public void reloadPrediction(View view) {
- 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
- Write an Android app with a textview that displays a random Magic8Ball
prediction when the linear layout is clicked. Design and use a Prediction class that has
two instance methods addPrediction and getPrediction.
- See at the Crazy8Ball Example
Project 1b.
- Look at Project 1b.
InnerClass Example
- Look at the InnerClass Example.
- Android event handlers can also be implemented with inner classes.