A Short Introduction to Android Programming
Essential Links
- Excellent step-by-step, (almost) fail-proof installation guide for the
Android development tools
- Android Developers should be your primary source of information. Among other things, it contains the following essential sections:
- Tutorials in the Resources section (many of the tutorials below are from this website.
- Sample code for going beyond tutorials (also in the Resources section)
- Complete reference to the Android SDK (in the Reference section).
- Best Practices (in the Dev Guide section).
- Documentation for the Android Development Tools (in the Dev Guide section).
- Android Applications
- The NCSU Android wiki
- Frank Mueller's Android page.
Short Presentations
Following is a list of presentations that gradually introduce the
basic concepts in Android programming. The presentations are meant to
be taught as a short course, although they may help a self-studying
student as well.
The sequence of tutorials first builds toward an understanding of
the basic concepts in Android programming (via HelloWorld and
HelloViews), then follows up toward managing multiple activities and
network programming, first in Java, then adaptation to Android, by
building inter-operating server/client pairs (e.g., the Android client
can connect to another Android Server or to a desktop
server). Finally, the last presentation suggests that the students
will build a simple chat program that uses broadcast UDP to exchange
strings among the participants. All tutorials can be completed in
about 20-25 hours starting from basic Java programming especially if
help is available.
- Java review (ppt). Covers very
briefly some Java aspects.
- XML review (ppt). Covers very
briefly some XML aspects.
- Introduction (ppt). Covers
what is Android, what products are out there (a bit outdated),
the software stack.
- Hello World (ppt). Follows
the Hello
World tutorial on developer.android.com.
- Application
Fundamentals (ppt). Introduces
the concepts of activity, service, broadcast receiver, content
provider and intent.
- Graphical User Interface
(ppt). Introduces the notions of
layouts, widgets, menus.
- Hello Views Part 1
(ppt)
and
Hello Views Part 2
(ppt) follow the tutorials on developer.android.com, introducing linear layouts, relative layouts, table layout, tab layout, list view, date picker, the most common widgets (essential), and finally, a web view.
- Hello Localization
(ppt), follows the tutorial developer.android.com - it's primarily for localization, but it's also very useful for understanding alternate layouts (e.g., for portrait vs. landscape layouts).
- Introduction to Socket Programming
(ppt) is a basic introduction to TCP and UDP socket programming in Java by using a very simple client/server application.
- Hello Menus
(ppt) is a basic introduction to menus.
- Hello Activities
(ppt) is an introduction to communications between activities (two different methods).
- Hello Images
(ppt) is a simple introduction to calling somebody else's activity (taking a picture with the camera and displaying it in your activity).
- Socket Programming on Android
(ppt) is a very simple example of an Android UDP client that works with the Java server introduced above.
- Hello Threads
(ppt) introduces threads for handling an UDP server while maintaining a responsive interface.
- Chat (ppt) wraps up the short course by asking the students to implement a simple chat program based on what they learned. The chat program can be infinitely expanded for a full grade (and extra points) - most students went well above the basic program by adding menus, notifications, text-to-speech and speech-to-text as well as other bells and whistles.
All presentations can be used for non-commercial purposes. For any other uses, please ask for permission.
Sample Code
- BroadcastChatUI1
- Just UI (no network functionality)
- When users type a message, the message is just displayed in ListView
- BroadcastChat1
- Add UDP broadcast
- When users write a message, the message is sent using UDP broadcast
- A separate thread listen broadcast packets and update UI.
- chatSample
- Similar to BroadcastChat1, but smaller
- Does not properly handle the application lifecycle
- BroadcastChat2
- Add user name
- Add a separate Activity to allow users to type user name
- Using menu item, the additional Activity is invoked
- BroadcastChat3
- Add chat room ( message will be displayed when users are in same room)
- This application doesn't have functionality to find existing rooms
- Room name is displayed in customized application title (right top of the application)
- Incoming messages are parsed to check room name
- Make a simple message format ( command id + room name + message body )
- BroadcastChat4
- Add finding list of exiting rooms
- Join the room
- An example of text-to-speech based on this article. Test
code is available.
Common Problems
Following is a list of common problems encountered when going through the tutorials above:
- A very common problem was having the Eclipse plug-in fail to generate the
R.java file. This can be caused by one of any number of things
(sometimes Eclipse is pointing to the problem, sometimes being silent,
and sometimes just crashing). Common problems that would prevent the generation of R.java:
- Resource file names that have anything else than the small letters and numbers in their name;
- Improper xml syntax (e.g., forgetting to close a tag);
- Not specifying a width and height for some widgets;
- An easy way to force regeneration of R.java is to simply delete it.
- Project -> Clean and rebuild cleans up some of the weird states Eclipse gets in sometimes (restarting Eclipse also fixes some of these states too).
- Right click on a project -> Android Tools -> Fix Project Properties also fixes problems with the project in several instances.
- When all else fails, exchange the layout and/or resource with ones that you know that work (e.g., the one from HelloWorld) and add the elements back one by one.
- When everything seems to compile fine but not work the most likely
cause is a permission missing in AndroidManifest. Unfortunately, the
SDK documentation does not specify what permissions are needed for
what objects/functions in the SDK. Common ones to miss are
Internet, WiFi status, WiFi change, vibrator.
- Log is a very useful debugging tool especially for network applications.
Make sure you open an LogCat window (from the Window-> Show View->Other menu) and add a filter for your TAG. If you stop getting Log messages at some point, disconnect and reconnect the phone. The same LogCat window many times offers a clue when an application crashes (although sometimes is difficult to trace down the primary cause from the log message).
- The debugger in Eclipse works well as long as you declare your application "Debuggable" in the AndroidManifest.
- The most common reason for failing to open a socket is having the
socket already opened by a different instance before and not (yet)
closed. Catching the exception for opening the socket and printing the
error message will clarify this.
- OnDestroy() is not called when the application closes - sometimes it's called much later (20-30 seconds), so do not rely on it for closing the socket. Sometimes it's not called at all.
Other Resources
A quick Android HowTo from Aman Nijhawan
(pdf) (doc)
(some of the information is dated)
More Android HowTo
Short threading tutorial and another one