Android INFO1-CE9705
In-class Examples

Begin here after you have installed everything.

Getting started

  1. XML: all you need to know about the Extensible Markup Language
  2. Java. A new language usually requires one semester. This review will get us started in one week.
    1. Object. Create objects and other variables.
    2. Class. Create a class of objects, including its fields and methods. Create one object of the class.
    3. Subclass. Create a subclass using inheritance, and one object of the subclass. The Java keywords extends, protected, super; the annotation @Override.
    4. Anonymous inner class. Will be used to create a View.OnClickListener.
  3. Create an Eclipse project
    1. Hello: Create an Eclipse project that displays a hello message in a TextView object on the screen.
    2. Zip: save the project as a zip file.
    3. Text: six ways to produce debugging output and other text.
    4. GitHub: create a GitHub account and upload your project to a repository. Student accounts.
    5. Capture an image of just the app window, or of the entire emulator.
    6. Download the app to an Android phone.
  4. No Eclipse: create the project without Eclipse. Use the programs in the tools directory.
  5. Install everyone’s project in the emulator, one at a time.
  6. ApiDemos: a sample app that contains individual demonstrations of lots of features.
  7. Design patterns
    1. Plug a listener into a view. First example: plug a View.OnClickListener into a Button. (In the iOS world, a listener is called a target or a delegate.)
    2. Plug an Adapter into an AdapterView. Examples of AdapterViews: Spinner, GridView, Gallery, ListView.

Controls and Listeners

  1. Reference: give the Java code a reference to an object in the main.xml file.
  2. Listener: an object in main.xml calls a method in a Java file in response to a touch. Anonymous inner classes, View.OnClickListener.
  3. A listener for an internal event: the TextToSpeechActivity.java listener in ApiDemos. See text to speech and tts.
  4. Controls: CheckBox, RadioButton, Spinner, etc. They don’t do anything.
  5. Widgets. These controls do something.
  6. Programmatic: create exactly the same layout and widgets as above by calling Java methods, not by using XML. It ignores main.xml.
  7. Spinner. Make a Spinner do something.
  8. TimePicker and an extra thread. The threads communicate via a Handler and Messages.

Layouts

  1. Documentation: Android’s layout documentation and ApiDemos examples.
  2. AbsoluteLayout and the attributes android:layout_x, etc.
  3. FrameLayout: the simplest type of layout
  4. LinearLayout
  5. Nested LinearLayouts
  6. AbsoluteLayout
  7. RelativeLayout
  8. TableLayout: rows and columns of Views, like an HTML table.
  9. To do: move "inch" and "months" examples from Project to LinearLayout. Do the union jack of "Don't tread on Me" with a FrameLayout.

Two-dimesional still-life graphics

  1. Japan: classes Canvas and Paint.
  2. Drawable: package a triangle as a Drawable object.
  3. Triangle: fill in a triangle and then outline it. Class Path.
  4. America: draw with for loops. Draw an image file.
  5. Manhattan: apply transformations to a drawing: translate, scale, rotate.
  6. Hardware Acceleration doesn’t always work.
  7. HTML5: graphics programmed in JavaScript will run on both Android and iPhone.

Taps and touches

  1. Etch A Sketch: interface View.OnTouchListener and its method onTouch. Erases itself when the activity’s configuration changes; see “Change of Configuration” below.
  2. Tap: distinguish between single vs. double tap. An OnTouchListener and a GestureDetector.
  3. Fling: detect a horizontal or vertical swipe.
  4. Pinch: distinguish between pinch vs. spread with a ScaleGestureDetector.
  5. Other methods of interface GestureDetector.OnGestureListener: onLongPress, onScroll, etc.
  6. Gesture: recognize a gesture created by the Gesture Builder.

Graphics that respond to a touch

Three ways to call invalidate over and over while an animation is in progress:

  1. Animate: the last statement of the onDraw method of the View calls invalidate, triggering another call to onDraw.
  2. ObjectAnimator: the onAnimationUpdate method of the AnimatorUpdateListener calls invalidate.
  3. Pearl: the run method of the Runnable object calls invalidate. The run method then runs itself again by calling the postDelayed method (with an argument of 1/60) of a Handler. All of this happens in the main thread.
  4. TimePicker: the run method of a separate Thread calls the setText method of a TextView without any call to invalidate.
  1. Drawable: an object that can be drawn by calling its draw method.
  2. Touch: a touch-sensitive graphic.
  3. Animate: a touch-sensitive graphic that moves under its own power.
  4. ObjectAnimator: change the properties of the object while the animation is in progress.
  5. Pearl: a touch-sensitive graphic with mass and momentum.
  6. Phase: a movie made of frames with an AnimationDrawable object.

Orientation as an Example of a Change of Configuration; Fragments

  1. Orientation: portrait vs. landscape.
  2. Land: different resource files for different orientations.
  3. NonConfigurationInstance: a deprecated way of preventing the Etch a Sketch from erasing the drawing when the orientation changes.
  4. Fragment without its own user interface: save information to be read by the next instantiation of the Activity.
  5. Fragment with its own user interface.
  6. Configuration: detect a configuration change without restarting the activity.

Dialogs

Press the buttons in ApiDemos/App/Alert Dialogs.

  1. TimePickerDialog pops us a TimePicker and then makes it disappear.
  2. Class DatePicker. Its tutorial has a DatePickerDialog. The demo ApiDemos/Views/Date Widgets/1. Dialog is more complicated than the TimePicker demo in ApiDemos because the DatePicker is mounted in a DatePickerDialog.
    1. DateWidgets1.java
    2. date_widgets_example_1.xml
  3. AlertDialog with three buttons.
  4. Digression: create the AlertDialog as a DialogFragment. See below.
  5. List dialog: an AlertDialog with a list of items.
  6. ProgressDialog: display a horizontal thermometer or a rotating ring.
  7. AsyncTask provides another way for threads to communicate.
  8. Dialog with an EditText ApiDemos/App/Alert Dialogs/Text Entry dialog
    1. AlertDialogSamples.java: Case DIALOG_TEXT_ENTRY uses a LayoutInflator to inflate the file alert_dialog_text_entry.xml. See ApiDemos/App/Menu/Inflate from XML.
    2. alert_dialog_text_entry.xml: a LinearLayout containing TextViews and EditTexts
    3. strings.xml
  9. Fragment. The onCreateDialog and onPrepareDialog methods of class Activity are deprecated. See Alert Dialog and ApiDemos/App/Fragment/Alert Dialog:
    1. FragmentAlertDialog.java
    2. fragment_dialog.xml
    3. strings.xml
    4. alert_dialog_icon.png
    Fragment documentation: Fragment, class Fragment, class DialogFragment.

Menus

An options menu is what you get when you press the Android menu button. It will usually be the same menu throughout the app. A context menu is what you get when you hold your finger long enough on a view on the screen. It will usually be a different menu for different views at different times.

  1. Options Menu. Create the menu from a resource in an .xml file. Display items in ActionBar in Android ≥ 3.0.
  2. Dynamic Menu. Create the MenuItems of an options menu by calling Java methods. Update the menu in onPrepareOptionsMenu.
  3. Context Menu.
  4. Submenu
  5. Shortcut keys for the options menu.
  6. Menu groups.

Intents and Activities

Activating Components. Intents and Intent Filters. Can I Use this Intent? Activity and Task Guidelines. <intent-filter> tag in AndroidManifest.xml.

  1. Finish: an Activity that kills itself.
  2. Fish: use an Intent object to go on a fishing trip for the names of all the activities in all the apps in the phone.
  3. Start: one Activity starts another Activity.
  4. Intents in ApiDemos: an intent with a MIME type
  5. Result: one Activity returns a result to another Activity.
  6. Intent: server and client.
  7. Igpay: the other Activity is Voice Search.

TabActivity, GridView, and Gallery

  1. Tab: the Android equivalent of an iPhone UITabBarController. Multiple Activity objects, too.
  2. Fragment Tab.
  3. GridView: rows and columns of Views.
  4. Gallery: just one row of Views.

Contacts and Cursors

Some of the ListView examples in ApiDemos read the Contacts database.

  1. Create a contact in Android 4.0.
  2. Cursor: loop through the contacts with a Cursor.
  3. A ListView that uses a Cursor is here.

ListView

  1. ListView: the Android equivalent of an iPhone UITableView.
  2. CursorAdapter: display the contacts with a Cursor in a SimpleCursorAdapter.
  3. ListView in ApiDemos.
  4. Tree: display a tree containing smaller trees.
  5. Modify the tree in response to user input.
  6. The tree that forms the backbone of ApiDemos.

SQLite

NotePad Tutorial and NotePad Sample app.

  1. SQLite interpreter. Launch the sqlite3 interpreter that you already installed in your tools directory.
  2. Tables: a database with two tables.
  3. Write and read an SQLite database.
  4. Zipcode: a ListView displaying a database you created from a csv file with sqlite3.
  5. Zipcode2: same as previous example, except that the database is downloaded from a server.
  6. Distance between two points. No easy way to do this in Android because Android does not have sqlite3_create_function.
  7. Blob

Services

  1. Service.
  2. AudioService: a Service that plays a sound file in a separate thread.
  3. AIDL: Android Interface Definition Language

Read and write a file

  1. File i/o.
  2. Preferences: save and restore the application preferences.
  3. Busybox: a set of Unix command-line utilities for the Android Emulator.

Audio and video

  1. Play audio with a MediaPlayer.
  2. Record audio with a MediaRecorder.
  3. SoundPool
  4. Play video.

Location and Google Maps

  1. Location: get the latitude and longitude.
  2. Map: the JavaScript interface to Google Maps.

OpenGL ES 2.0

See OpenGL.

  1. ApiDemos/Graphics/OpenGL ES/OpenGl ES 2.0: source code is GLES20Activity.java, GLES20TriangleRenderer.java, TriangleRenderer.java, res/raw/robot.png.

Miscellaneous

  1. NDK: the Native Development Kit lets an app call functions written in C or C++.

Still to do

  1. To do: ApiDemos as example of tree of ListViews.
  2. Arguments and return value for NDK function.
  3. Igpay: add thr-. Better yet, s/[bcdfghjklmnpqrstvwxyz]+[a-z]+/\2\1ay/
  4. Download zipcode database from web in AsyncTask.