Android
INFO1-CE9705
In-class Examples
Begin here after you have
installed
everything.
Getting started
- XML:
all you need to know about the Extensible Markup Language
- Java.
A new language usually requires one semester.
This review will get us started in one week.
- Object.
Create objects and other variables.
- Class.
Create a class of objects, including its fields and methods.
Create one object of the class.
- Subclass.
Create a subclass using inheritance, and one object of the subclass.
The Java keywords
extends
,
protected
,
super
;
the annotation
@Override
.
- Anonymous inner class.
Will be used to create a
View.OnClickListener
.
- Create an Eclipse project
- Hello:
Create an Eclipse project
that displays a hello message in a
TextView
object on the screen.
-
Zip:
save the project as a zip file.
-
Text:
six ways to produce debugging output and other text.
- GitHub:
create a GitHub account
and upload your project to a repository.
Student accounts.
- Capture an image
of just the app window,
or of the entire emulator.
- Download
the app to an Android phone.
- No Eclipse:
create the project without Eclipse.
Use the programs in the
tools
directory.
- Install
everyone’s project in the emulator, one at a time.
- ApiDemos:
a sample app that contains individual demonstrations of lots of features.
- Design patterns
- 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.)
- Plug an
Adapter
into an
AdapterView
.
Examples of
AdapterView
s:
Spinner
,
GridView
,
Gallery
,
ListView
.
Controls and Listeners
- Reference:
give the Java code a reference to an object in the
main.xml
file.
- Listener:
an object in
main.xml
calls a method in a Java file
in response to a touch.
Anonymous inner classes,
View.OnClickListener
.
- A listener for an internal event: the
TextToSpeechActivity.java
listener in ApiDemos.
See
text
to speech
and
tts.
- Controls:
CheckBox
,
RadioButton
,
Spinner
,
etc.
They don’t do anything.
- Widgets.
These controls do something.
- Programmatic:
create exactly the same layout and widgets as above by calling Java methods,
not by using XML.
It ignores
main.xml
.
- Spinner.
Make a Spinner do something.
- TimePicker
and an extra thread.
The threads communicate via a Handler and Messages.
Layouts
- Documentation:
Android’s layout documentation and ApiDemos examples.
AbsoluteLayout
and the attributes
android:layout_x
,
etc.
- FrameLayout:
the simplest type of layout
- LinearLayout
- Nested LinearLayouts
AbsoluteLayout
- RelativeLayout
- TableLayout:
rows and columns of
View
s,
like
an
HTML table.
- 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
- Japan:
classes
Canvas
and
Paint
.
- Drawable:
package a triangle as a
Drawable
object.
- Triangle:
fill in a triangle and then outline it.
Class
Path
.
- America:
draw with
for
loops.
Draw an image file.
- Manhattan:
apply transformations to a drawing:
translate, scale, rotate.
- Hardware
Acceleration
doesn’t always work.
- HTML5:
graphics programmed in JavaScript will run on both Android and iPhone.
Taps and touches
- Etch A Sketch:
interface
View.OnTouchListener
and its method
onTouch
.
Erases itself when the activity’s configuration changes;
see “Change of Configuration” below.
- Tap:
distinguish between single vs. double tap.
An
OnTouchListener
and a
GestureDetector
.
- Fling:
detect a horizontal or vertical swipe.
- Pinch:
distinguish between pinch vs. spread
with a
ScaleGestureDetector
.
- Other methods of interface
GestureDetector.OnGestureListener
:
onLongPress
,
onScroll
,
etc.
- Gesture:
recognize a gesture created by the Gesture Builder.
Graphics that respond to a touch
- Drawable:
an object that can be drawn by calling its
draw
method.
- Touch:
a touch-sensitive graphic.
- Animate:
a touch-sensitive graphic that moves under its own power.
- ObjectAnimator:
change the properties of the object while the animation is in progress.
- Pearl:
a touch-sensitive graphic with mass and momentum.
- Phase:
a movie made of frames with an
AnimationDrawable
object.
- Orientation:
portrait vs. landscape.
- Land:
different resource files for different orientations.
- NonConfigurationInstance:
a deprecated way of preventing the
Etch a Sketch
from erasing the drawing
when the orientation changes.
- Fragment
without its own user interface:
save information to be read by the next instantiation of the Activity.
- Fragment
with its own user interface.
- Configuration:
detect a configuration change without restarting the activity.
Press the buttons in
ApiDemos/App/Alert Dialogs.
- TimePickerDialog
pops us a TimePicker and then makes it disappear.
- 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
.
DateWidgets1.java
date_widgets_example_1.xml
- AlertDialog
with three buttons.
- Digression: create the
AlertDialog
as a
DialogFragment
.
See below.
- List dialog:
an
AlertDialog
with a list of items.
- ProgressDialog:
display a horizontal thermometer or a rotating ring.
- AsyncTask
provides another way for threads to communicate.
- Dialog with an
EditText
ApiDemos/App/Alert Dialogs/Text Entry dialog
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.
alert_dialog_text_entry.xml
:
a
LinearLayout
containing
TextView
s
and
EditText
s
strings.xml
- Fragment.
The
onCreateDialog
and
onPrepareDialog
methods of class
Activity
are deprecated.
See
Alert Dialog
and
ApiDemos/App/Fragment/Alert Dialog:
FragmentAlertDialog.java
fragment_dialog.xml
strings.xml
alert_dialog_icon.png
Fragment documentation:
Fragment,
class
Fragment
,
class
DialogFragment
.
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.
- Options Menu.
Create the menu from a resource in an
.xml
file.
Display items in ActionBar in Android ≥ 3.0.
- Dynamic Menu.
Create the
MenuItem
s
of an options menu by calling Java methods.
Update the menu in
onPrepareOptionsMenu
.
- Context Menu.
- Submenu
- Shortcut
keys
for the options menu.
- 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
.
- Finish:
an Activity that kills itself.
- 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.
- Start:
one Activity starts another Activity.
- Intents in
ApiDemos:
an intent with a MIME type
- Result:
one Activity returns a result to another Activity.
- Intent: server and client.
- Igpay:
the other Activity is Voice Search.
- Tab:
the Android equivalent of an iPhone
UITabBarController
.
Multiple
Activity
objects, too.
- Fragment Tab.
- GridView:
rows and columns of Views.
- Gallery:
just one row of Views.
Some of the
ListView
examples in ApiDemos read the Contacts database.
- Create
a contact in Android 4.0.
- Cursor:
loop through the contacts with a
Cursor
.
- A
ListView
that uses a
Cursor
is
here.
- ListView:
the Android
equivalent of an iPhone
UITableView
.
- CursorAdapter:
display the contacts with a
Cursor
in a
SimpleCursorAdapter
.
- ListView in
ApiDemos.
-
Tree:
display a tree containing smaller trees.
- Modify the
tree
in response to user input.
- The tree that forms the backbone of ApiDemos.
NotePad
Tutorial
and
NotePad
Sample app.
- SQLite
interpreter.
Launch the
sqlite3
interpreter
that you already
installed
in your
tools
directory.
- Tables:
a database with two tables.
- Write and read
an SQLite database.
- Zipcode:
a
ListView
displaying a database you created from a csv file with
sqlite3
.
- Zipcode2:
same as previous example, except that the database is downloaded
from a server.
- Distance
between two points.
No easy way to do this in Android because Android does not have
sqlite3_create_function
.
- Blob
- Service.
- AudioService:
a Service that plays a sound file in a separate thread.
- AIDL:
Android Interface Definition Language
Read and write a file
- File i/o.
- Preferences:
save and restore the application preferences.
- Busybox:
a set of Unix command-line utilities for the Android Emulator.
Audio and video
- Play audio
with a
MediaPlayer
.
- Record audio
with a
MediaRecorder
.
- SoundPool
- Play video.
Location and Google Maps
- Location:
get the latitude and longitude.
- Map:
the JavaScript interface to Google Maps.
OpenGL ES 2.0
See
OpenGL.
- ApiDemos/Graphics/OpenGL ES/OpenGl ES 2.0:
source code is
GLES20Activity.java
,
GLES20TriangleRenderer.java
,
TriangleRenderer.java
,
res/raw/robot.png
.
Miscellaneous
- NDK: the
Native Development Kit lets an app call functions written in C or C++.
Still to do
- To do: ApiDemos as example of tree of ListViews.
- Arguments and return value for NDK function.
- Igpay: add thr-.
Better yet,
s/[bcdfghjklmnpqrstvwxyz]+[a-z]+/\2\1ay/
- Download zipcode database from web in AsyncTask.