This repo contains about seventy source code examples from the recipes published in the O’Reilly Android Cookbook (see http://androidcookbook.com/), a great source of tutorial material for the Android Developer. Not all recipes in the book or online have code accompanying them; this repo features code that was either linked by the contributor, or in a few cases, re-constructed as a New Project by the editor, for your convenience.
Further note that not all programs used in the book/online appear here; some of the examples in the book/online are only program fragments taken from other programs that can’t be included. And of course some code is drawn from existing projects that are hosted elsewhere.
Each Directory comprises its own project, and has a name that is cited at the end of the corresponding Recipe, under the "Source Code Download" section.
NOTE As of today, there are two styles of projects:
-
Eclipse projects with the newer Eclipse Andmore;
-
Android Studio projects (most complete with their extraneous "app" subdirectory).
If you have used the old Google ADT, you will need to update your Eclipse config by removing ADT, then replace it with AndMore from the Eclipse Marketplace Client.
Some of the Eclipse projects have been Mavenized and some have not.
The table at the bottom of this page shows which projects are set up for which build tools.
If you open the entire repo in Eclipse with eGit, be sure to check the Import all projects
checkbox. If you want to keep these mini-projects separate from your "regular" work, you may want to use the Git repo as an Eclipse workspace (use File→Switch Workspace→Other→Browse).
This directory is already set up to be an Eclipse workspace if you want it to
(the .metadata directory exists and is already in .gitignore).
Obviously the Googlezilla has spoken and we will have to migrate kicking and screaming to Android Studio and Gradle; pull requests for this would also be welcomed, as long as they don’t break support for Eclipse and Maven.
You can download this as a Zip file, but then you won’t get updates, and it will be hard to collaborate with the other contributors. If you’re new to Git, see the page on GitHub about how to collaborate. You can either send diff/patch files or send a pull request. Remember that GitHub is a free service for public projects!
If you open the repo as an Eclipse Workspace and no projects appear, despair not! Just do File→Import…→General→Existing Projects Into Workspace, browse to the top of the repo (the list of projects should appear), be sure "Import projects into workspace" is NOT checked, and click Finish; wait a bit.
Many of the projects may (depending on your version of Eclipse) give you some noise when you first open them due to bin and/or gen being missing. The Eclipse plug-in will usually create them but not notify that it’s done so, meaning you just have to do Refresh (F5), failing that, close and re-open the project. Does not happen with current Eclipse, last I tried.
There is neither a Wiki or a Tracker for this project, since bugs and suggestions about the code should be made on the Android Cookbook web site (http://androidcookbook.com/). Bugs or suggesions on the published edition of the book should go to http://shop.oreilly.com/product/0636920010241.do; you can also purchase the book there (which helps fund my continued work on this code base).
The current version of this repo is being reorganized in preparation for a new edition of the book. There have been quite a few projects added since the first published edition of the book, and a few renamed, so if you have the printed edition prior to December, 2012 you should rummage around here before re-typing anything that looks like a full example.
If you want this repo to correspond to the last minor revision of the first edition,
git checkout the tag Book-1.0.
There are some things I’d love it if people would contribute:
-
Maven and/or Gradle files for each project that doesn’t already have one: always automate dependencies where possible!
-
Readme file for each project, to include a link back to the recipe in the book.
The following list is generated (periodically) from the project folders. In the Builds With column, E for Eclipse, M for Maven, S for Android Studio, G for Gradle.
| Project | Builds With | Function | Notes |
|---|---|---|---|
E |
Info about device and screen |
- |
|
E |
Graphing |
- |
|
E |
RSS Reader |
- |
|
SG |
Installs another app |
Downloading, package management |
|
E |
App can update itself |
see AppDownloader |
|
E |
Contact picker with autocomplete |
- |
|
E |
Backups |
- |
|
E |
Bluetooth Connection |
- |
|
E |
Add appointment to Calendar |
Calendar via Content Provider |
|
E |
Pretend a call happened, for testing |
- |
|
E |
GUI demo |
name says it |
|
EM |
List of bookmarks |
Browser ContentProvider |
|
E |
List of ContentProviders |
- |
|
E |
Implement a ContentProvider |
- |
|
E |
Test it |
- |
|
E |
Time-based activity |
- |
|
M |
Application Widget |
- |
|
EM |
How to load data properly using CursorLoader |
- |
|
E |
Menu |
- |
|
E |
Toast |
- |
|
E |
Dialogs |
- |
|
E |
Demo of "Dreams" (4.x screensaver) |
- |
|
E |
Send contents of EditText via email |
- |
|
E |
Attach file to email |
- |
|
E |
Java + JavaScript |
- |
|
E |
Face locator API in photos |
- |
|
E |
Access FB API |
- |
|
E |
Fake up a Cursor from a list of files |
INCOMPLETE |
|
E |
Using a custom font |
- |
|
E |
Simple photo gallery |
Deprecated |
|
E |
Google Cloud Messaging - receiver |
- |
|
E |
Google Cloud Messaging - sender |
- |
|
E |
HelloWorld |
SDK |
|
E |
HelloWorld |
Eclipse ADT |
|
EM |
HelloWorld |
Maven, JayWay Plug-in |
|
E |
ADT Testing - subject |
- |
|
E |
ADT Testing - Tests |
- |
|
EM |
A Home Application |
Shows running a program |
|
E |
Add a contact |
Shows ContactsContract, content provider |
|
E |
List View with Icon |
- |
|
EM |
Intents |
- |
|
SG |
Demos of java.time date/time API |
Actually JSR-310 API; almost same but different package |
|
E |
JavaScript from Java |
- |
|
- |
This app has moved to its own repo |
||
E |
ListView |
- |
|
E |
ListView |
- |
|
E |
ListView with empty list: don’t show blank screen! |
- |
|
E |
ListView with section headers |
- |
|
E |
Local Broadcast Receiver |
- |
|
E |
Google Maps V2 API |
- |
|
E |
Search the Play Store |
- |
|
E |
Play media |
- |
|
E |
Simple sound recorder |
- |
|
E |
Native Development Kit |
C Code |
|
E |
Notifications |
- |
|
E |
UI for picking numbers |
- |
|
EM |
OpenStreetMap API |
(free alternative to Google Maps) |
|
E |
OpenGL graphics |
- |
|
E |
Rotation |
- |
|
E |
Create and share a PDF |
- |
|
E |
HTML5 app using phonegap/cordova |
- |
|
E |
Preferences |
- |
|
E |
Using a graphing library |
- |
|
E |
Using a graphing library |
- |
|
E |
How to reboot, and why you can’t |
- |
|
EM |
SQLite demos |
- |
|
Multi-screen |
- |
||
E |
Sends an SMS |
- |
|
E |
ICS "Share" action |
- |
|
E |
User account on device |
- |
|
E |
Calendar |
- |
|
Flixel-based gaming demo |
This example still needs to be re-created. |
||
E |
Graphics |
- |
|
E |
Flashlight/torch from camera flash |
- |
|
E |
The "Sliding Drawer" view |
- |
|
EM |
Snackbar (bottom message text) demo |
- |
|
E |
XML SOAP Web Service |
- |
|
EM |
Sound Recorder |
- |
|
E |
Text To Speech (TTS) |
- |
|
EM |
Drop-down Spinner Demos |
- |
|
E |
Splash screen |
- |
|
E |
Use Existing Drawables |
- |
|
E |
Tabbable and swipeable layout |
- |
|
E |
Telephone call recorder (in progress!) |
- |
|
E |
Displays phone info |
- |
|
E |
Full app: compute and show restaurant bill with tip |
- |
|
Todo Sync Client |
Moved to own repo |
||
E |
Find device’s unique ID |
- |
|
E |
Make the device vibrate |
- |
|
E |
Swipe among views |
- |
|
EM |
Demo of Volley networking library |
- |
|
E |
Graphical window background |
- |
|
E |
Compat library Eclipse Project |
- |
|
E |
Grid Layout Compat Library for Eclipse |
- |