To Lecture Notes
IT 372 -- May 15, 2019
Review Exercises
- How do you dynamically add a view to the layout?
Ans: If the id of the layout is created like this in the XML layout file:
android:id="@+id/layout"
and a Java layout object is obtained from its id like this in the activity code:
LinearLayout layout = (Layout) findViewById("R.id.layout");
add the widgit w to the layout like this:
layout.addView(w);
- If you want to be able to draw on a view, why does the view need to be dynamically added to the layout?
Ans: Because the view is created as a derived class of view, and
its onDraw method is overridden. Because an instance of this view is
represented as a Java object, it is added to the layout as in Exercise 1.
- How does the Touch event work?
Project 4 -- Realestate App
- Look at the Project 4 Description.
- Here are the main window and
detail dialog from the
.Net desktop app that motivated this project.
Practice Problems
The answers are posted in the table at the end of this section.
- In a view derived from the View class (see the
TouchEvent1 Example), display a disk at the
last location touched when the touch is released.
- Modify Problem 1 so that all of the touches are marked with disks.
- Add a button to the app in Problem 2 that clears the disks from the view.
- Modify Problem 3 so that locations of circles read from a database are displayed
in the onCreate method of the activity. Here is the database table:
id (integer) | xposition (float) | yposition (float) |
1 | 100 |
50 |
2 |
75 |
130 |
3 |
230 |
300 |
4 |
80 |
170 |
5 |
30 |
30 |
- Set up an event handler so that when one of the five discs drawn by the
database is touched, the id of the row is displayed in the Logcat or Run window.
Alternatively, show the ID in a toast.
Answers:
Here is the DBRow.java file also used in Problem 5.