App Specs: Create a Java class named
NoteableItems , objects of which contain an ArrayList
collection of notable items (e.g., quotations, jokes, riddles, or interesting
facts represented by strings). Your class should also have an instance method such
as getItem that returns a random item. Also include a
method that adds an item to the collection, for example, addItem. See the
Magic8Ball Example. When your class is working, use
it in an app to display a random a random item in a TextView
control. Use the LinearLayout layout. When your app is
able to display the random item, add an event handler so that a random item is
redisplayed when the linear layout is clicked. The random quote should be
chosen from a sample set of at least 8 noteable items, but not more than 12.
When you implemement the event handler, use Method 4, not Method 1. (We will discuss methods 1, 2, 3, and 4 on Wednesday,
Apr 9.)
Method 1 uses an onClick attribute in the layout file, which is deprecated. Method 4 adds the the event handler, also called an event listener, in the onCreate method
in the MainActivity class. The event handler is written as an anonymous method in arrow notation.
Here is the idea:
// Define the id for a button
// in the layout file like this:
android:id="@+id/btn1"
// Add the event listener for the button
// in the onCreate method.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Other onCreate code goes here
// Code that adds the event listener
// to the button.
Button button1 = findViewById(R.id.btn1);
button1.setOnClickListener( (view) -> {
// body of onclick listener goes here.
});
}
Display the app title at the top of your app. The title string should be
defined in the string.txt file with the name
"app_name".
Do not include a
main method in your NoteableItem
class. The current version of Android Studio does run main methods.
Set properties of the LinearLayout and
TextView controls to set colors and position the widgets.