This repo contains over a hundred source code examples from the recipes published in the O’Reilly Android Cookbook, 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 several 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.
Most of the Eclipse projects have been Mavenized; a few have not.
Most of the Gradle projects will also build with Android Studio; a few will not.
The table List of Projects shows which projects are set up for which build tools.
Prerequisites: Java 7 SDK including "Google Repository" for compat stuff, git, Maven
-
git clone https://github.com/mosabua/maven-android-sdk-deployer
-
cd maven-android-sdk-deployer
-
mvn install -P 6.0 # at least
-
mvn install -P 7.0 # after it’s released!
-
cd ..
-
git clone https://github.com/IanDarwin/Android-Cookbook-Examples
-
cd Android-Cookbook-Examples
-
mvn -fae package android:install android:run Include android:install and android:run only if you actually want to run them all, and have a device or emulator with some good free space; else just do 'package')
-
Fix up any that didn’t work, and send pull requests!
Most of these projects were initially developed using Eclipse ADT, and migrated to Maven for automatic building. Obviously the Googlezilla has spoken and we will eventually 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.
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).
Note: As part of the move to Gradle, Google stopped pushing Android releases to Maven Central; their last version was 4.1.1.4, thus Lollipop, Marshmallow, and "N" have been left high-and-dry. And worse, their SDK license prohibits re-distribution of binaries, so nobody else can step in and upload these files to Maven Central. But of course there is a solution. To compile these projects with Maven you will need to download the amazing maven-android-sdk-deployer, which repackages files from the SDK - after you download it using the Android tools, thus nobody is redistributing it - into your local .m2/repository so that Maven can find it. This is the function of the first few steps in the numbered list above.
Note that projects using official releases use e.g. com.google.android:android:4.1.1.4 whereas projects deployed using the sdk-deployer use e.g android:android:6.0_r3.
You can download AndroidCookbookExamples as a Zip file from GitHub, but then you won’t get updates, and it will be hard to collaborate with the other contributors. If you’re not familiar with Git and Github, see the getting started page on GitHub and read up on 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!
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).
You can’t have this pretty thing. Because Gradle. Because Android Studio. Because fail.
Patches welcome if they don’t break any existing builds (Maven, Eclipse) AND if you can still open each individual project without Studio going ape-** and opening every other existing project as a "module" of the one you’re trying to build. Watch for my forthcoming paper "Gradle and Android Studio Considered Harmful."
The current version of this repo has been reorganized in preparation for the second 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 from 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,
use git to checkout the tag Book-1.0
.
JpsTrack and the ToDo applications have graduated to their own github projects, up one level from here on Github.
There are some things I’d love it if people would contribute:
-
Gradle files for each project that doesn’t already have one (they almost all have Maven files now.
-
Readme file for each project, to include a link back to the recipe in the online version of 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 |
---|---|---|---|
GS |
A re-usable About dialog. |
- |
|
EM |
Info about device and screen |
- |
|
GS |
Simple ActionBar demo(s) |
- |
|
EM |
Graphing |
- |
|
EM |
RSS Reader |
- |
|
MGS |
Installs another app |
Downloading, package management |
|
EM |
Contact picker with autocomplete |
- |
|
EM |
App can update itself |
see AppDownloader |
|
EM |
Backups |
- |
|
EM |
Bluetooth Connection |
- |
|
EM |
Add appointment to Calendar |
Calendar via Content Provider |
|
EM |
Pretend a call happened, for testing |
- |
|
GS |
Demonstation of Card widget. |
- |
|
EM |
GUI demo |
name says it |
|
EM |
List of bookmarks from Bookmarks Content Provider |
Browser ContentProvider |
|
EM |
List of ContentProviders |
- |
|
EM |
Implement a ContentProvider |
- |
|
EM |
Test it |
- |
|
EM |
Time-based activity |
- |
|
M |
Application Widget |
- |
|
EM |
How to load data properly using CursorLoader |
- |
|
EM |
Menu |
- |
|
GS |
Submenu, created programmatically |
- |
|
EM |
Toast |
- |
|
EM |
Create a database from an SQLite .db file in 'assets'. |
- |
|
EM |
Fake up a Cursor from a list of files |
- |
|
EM |
Dialogs |
- |
|
E |
A simple Drag-and-drop example. |
- |
|
EM |
Demo of "Dreams" (4.x screensaver) |
- |
|
EM |
Send contents of EditText via email |
- |
|
EM |
Attach file to email |
- |
|
EM |
Java + JavaScript |
- |
|
M |
5 Ways to Wire an Event Listener. |
- |
|
EM |
Access FB API |
- |
|
EM |
Face locator API in photos |
- |
|
GS |
Demo of FileProvider. |
- |
|
MGS |
Internal and external files. |
- |
|
GS |
Demo of 'fab' Floating Abstract Button |
- |
|
EM |
Using a custom font |
- |
|
GS |
Simple Fragment and List-Detail Fragments in one project |
- |
|
EM |
Simple photo gallery |
Deprecated |
|
EM |
Google Cloud Messaging - receiver |
- |
|
EM |
Google Cloud Messaging - sender |
- |
|
EM |
HelloWorld |
SDK |
|
EM |
HelloWorld |
Eclipse ADT |
|
GS |
HelloWorld of EspressoTesting |
- |
|
GS |
Hello World with Gradle but NOT Studio. |
- |
|
EM |
HelloWorld |
Maven, JayWay Plug-in |
|
GS |
HelloWorld of StudioTesting |
- |
|
EM |
ADT Testing - subject |
- |
|
EM |
ADT Testing - Tests |
- |
|
EM |
A Home Application |
Shows running a program |
|
EM |
Add a contact |
Shows ContactsContract, content provider |
|
EM |
List View with Icon |
- |
|
EM |
Intents |
- |
|
GS |
IPC - Inter-Process Communication, simplified demo. |
- |
|
EM |
JavaScript from Java |
- |
|
MGS |
Demos of java.time date/time API |
Actually JSR-310 API; almost same but different package |
|
EM |
ListView |
- |
|
EM |
ListView |
- |
|
EM |
ListView with empty list: don’t show blank screen! |
- |
|
EM |
ListView with section headers |
- |
|
EM |
Local Broadcast Receiver |
- |
|
EM |
Google Maps V2 API |
- |
|
EM |
Search the Play Store |
- |
|
EM |
MaterialDesign demos |
- |
|
EM |
Play media |
- |
|
E |
Media Player demo from Marco Dinacci, thanks. |
- |
|
EM |
Simple sound recorder |
- |
|
EM |
Native Development Kit |
C Code |
|
EM |
Notifications |
- |
|
EM |
UI for picking numbers |
- |
|
GS |
Demo of OAuth2 to Google Tasks. |
- |
|
EM |
OpenGL graphics |
- |
|
EM |
Rotation |
- |
|
EM |
OpenStreetMap API |
(free alternative to Google Maps) |
|
EM |
Create and share a PDF |
- |
|
G |
Shows asking for permissions at runtime, i.e,. "the new way" |
- |
|
EM |
HTML5 app using phonegap/cordova |
- |
|
GS |
Pinch-and-zoom graphics demo |
- |
|
EM |
Preferences |
- |
|
EM |
Using a graphing library |
- |
|
EM |
How to reboot, and why you can’t |
- |
|
GS |
Demo of RecyclerView, the faster ListView. |
- |
|
EM |
Using a graphing library |
- |
|
EM |
A simpe runtime log facility |
- |
|
M |
Trivial example of one Activity (screen) starting another. |
- |
|
EM |
Sends an SMS |
- |
|
EM |
ICS "Share" action |
- |
|
EM |
User account on device |
- |
|
EM |
Calendar |
- |
|
M |
Flixel-based gaming demo |
This example still needs to be re-created. |
|
EM |
Graphics |
- |
|
EM |
Flashlight/torch from camera flash |
- |
|
EM |
The "Sliding Drawer" view |
- |
|
EM |
Snackbar (bottom message text) demo |
- |
|
EM |
XML SOAP Web Service |
- |
|
EM |
Sound Recorder |
- |
|
EM |
Text To Speech (TTS) |
- |
|
EM |
Drop-down Spinner Demos |
- |
|
E |
OpenGL Spinning Cube, from Marco Dinacci |
- |
|
EM |
Splash screen |
- |
|
EM |
SQLite demos |
- |
|
EM |
Use Existing Drawables |
- |
|
EM |
StaticFileRead - read a file from the application |
- |
|
EM |
Tabbable and swipeable layout |
- |
|
EM |
Telephone call recorder (in progress!) |
- |
|
EM |
Displays phone info |
- |
|
EM |
Full app: compute and show restaurant bill with tip |
- |
|
EM |
Find device’s unique ID |
- |
|
EM |
Make the device vibrate |
- |
|
EM |
Swipe among views |
- |
|
EM |
Demo of Volley networking library |
- |
|
GS |
Apple-style Wheel Picker Demo |
- |
|
EM |
Graphical window background |
- |