To Lecture Notes

IT 372 -- May 15, 2019

Review Exercises

  1. 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);
    
  2. 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.
  3. How does the Touch event work?

Project 4 -- Realestate App

Practice Problems

The answers are posted in the table at the end of this section.

  1. 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.
  2. Modify Problem 1 so that all of the touches are marked with disks.
  3. Add a button to the app in Problem 2 that clears the disks from the view.
  4. 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
  5. 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:

Problem Layout Activity MyView DBHelper
     1 activity_main.xml MainActivity.java MyView.java  
     2 activity_main.xml MainActivity.java MyView.java  
     3 activity_main.xml MainActivity.java MyView.java  
     4 activity_main.xml MainActivity.java MyView.java PointsDBHelper.java
     5 activity_main.xml MainActivity.java MyView.java PointsDBHelper.java

Here is the DBRow.java file also used in Problem 5.