To Lecture Notes
IT 372 -- Apr 15, 2024
Specs for Projects
When you create your project, name it Proj1aSmith (replace 1a by the
actual project number and Smith by your last name).
- The package that you set when creating your project can be
it372 so that the import statement at the top
of your Java files will be
import it372.proj1asmith;
- Change the title of your app in the strings.xml resource file.
Display the title at the top of your app in a TextView
widget.
- Choose interesting colors and layouts for your projects. We will see how to display an image in
an ImageView control today. For Project 1b or Project 2, you can improve your score for
creativity by adding an image to your apps.
- Extract colors and string constants to the resource files colors.xml and
strings.xml.
- Both the layout and activity files must have source code comments.
- Both the layout and activity files must have headers that look like
this:
In layout file:
<?xml version="1.0" encoding="utf-8"?>
<!-- Stan Smith
Project 1a
Apr 12, 2021 -->
In activity file:
// Stan Smith
// Project 1a
// Apr 12, 2021
- Use Android Studio to create the zipfile of your project.
Practice Exercises
- How does the Android app know which layout file to use?
Ans: It looks in the MainActivity.java file in the
onCreate method:
setContentView(R.layout.activity_main);
This statement
tells the activity that activity_main.xml is the layout
file.
- How do you start an Android app from the virtual device?
Ans: On the Galaxy Nexus API 22 virtual device, swipe up to view the installed
apps. For some versions you click on this icon to show the installed apps:
.
Then scroll up or down to find the icon of the app you want to run and click it.
- How do you uninstall apps from the Android virtual device?
Ans: Click up to view the installed apps, then scroll to find the Settings Icon and click it.
Scroll down to find Apps and click it. Scroll
down to find Apps & notifications and click it. Click on SEE ALL 18 APPS, scroll
to find the app you wish to uninstall, click on the App, click UNINSTALL.
- Look at the complete Magic8Ball Example,
complete with all 20 possible predictions.
If you have an Android phone, you may wish to install this app or other apps on your phone.
To do this, turn on
Developer options using these directions:
https://developer.android.com/studio/debug/dev-options
(You really must tap the Build Number option 7 times to enable Developer options).
Then connect your phone to your PC with a USB cable and run the app in the Emulator. This will simultaneously
install and run the app on your phone.
- Look at the section Adding a ScrollView found
below in these notes. Then display the following in a linear layout with a
scrollview.
- Display the numbers from 1 to 1000 in a textview control, one number per line.
Add a ScrollView to allow the user to swipe up and down to view all the numbers.
Answer: Here are the layout and activity files:
activity_main.xml
MainActivity.java
- Start with some Lorem Ipsum text in a textview widget:
Lorem ipsum dolor sit amet,
consectetur adipiscing elit.
In nibh lectus, accumsan
at ligula sagittis, bibendum
suscipit enim.
When the textview widget is clicked, concatenate 100 copies of this paragraph
together and display the new repeated text in the textview. Add scrollbars so the
user can see the entire textview.
Answer: Here are the layout and activity files:
activity_main.xml
MainActivity.java
Android Event Handlers
- Android event handlers can also be implemented with inner classes.
- The details are in the TempConverter Example
- Method 1 is to use an onClick attribute as we have been doing.
- Method 2 is to use an inner class to implement the click event handler.
- Method 3 is to use an anonymous inner class to implement the click event handler.
- Method 4 (recommended method) is use the setEventListener method to add an anonymous
event handler method defined with arrow notation. Unlike JavaScript that
uses a fat arrow ( => ) to define a method defined with
arrow notation, Java uses a thin arrow ( -> ).
- The View.OnClickListener class is an inner class of the
View class.
Using Images
- Setting a background image:
- Use this Blackhole Image as the background
image for an Android app.
- You can also display an image in an ImageView widget.
Adding a ScrollView
Project 2
BeerAdvisor Example
Array Adapters
- Practice Problems
- How to add items programatically to a
Spinner widget? Use an ArrayAdapter object.
Answer: Add a Spinner widget to the layout, with id set to
spinner1. Then
- place the array of Integer objects to display in the spinner in an array list,
- set up an ArrayAdapter object that connects the spinner to the array list.
Here is the Java code:
// Set up array list.
ArrayList<Integer> arrList = new ArrayList<Integer>( );
for(int n = 1001; n <= 1005; n++) {
arrList.add(n);
}
// Set up array adapter.
ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(
this, android.R.layout.simple_list_item_1, arrList);
spinner1.setAdapter(adapter);
- ChineseHoroscope Example:
- Input a year of birth using a Spinner widget. Use an
array adapter to load the years into the spinner.
- Determine the Chinese horoscope animal corresponding to the year.
The animal number can be obtained from year % 12.
- Display the animal in a TextView widget.
Answer: The layout and activity files:
activity_main.xml
MainActivity.java