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.
Most of the Eclipse projects have been Mavenized; a few have not.
The table at the bottom of this page 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).
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
.
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 |
---|---|---|---|
EM |
Info about device and screen |
- |
|
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 |
- |
|
EM |
GUI demo |
name says it |
|
EM |
List of bookmarks |
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 |
- |
|
EM |
Toast |
- |
|
EM |
Fake up a Cursor from a list of files |
- |
|
EM |
Dialogs |
- |
|
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 |
- |
|
MGS |
Internal and external files. |
- |
|
EM |
Using a custom font |
- |
|
EM |
Simple photo gallery |
Deprecated |
|
EM |
Google Cloud Messaging - receiver |
- |
|
EM |
Google Cloud Messaging - sender |
- |
|
EM |
HelloWorld |
SDK |
|
EM |
HelloWorld |
Eclipse ADT |
|
G |
Hello World with Gradle but NOT Studio. |
- |
|
EM |
HelloWorld |
Maven, JayWay Plug-in |
|
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 |
- |
|
EM |
JavaScript from Java |
- |
|
MGS |
Demos of java.time date/time API |
Actually JSR-310 API; almost same but different package |
|
- |
This app has moved to its own repo |
||
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 |
Play media |
- |
|
EM |
Simple sound recorder |
- |
|
EM |
Native Development Kit |
C Code |
|
EM |
Notifications |
- |
|
EM |
UI for picking numbers |
- |
|
EM |
OpenGL graphics |
- |
|
EM |
Rotation |
- |
|
EM |
OpenStreetMap API |
(free alternative to Google Maps) |
|
EM |
Create and share a PDF |
- |
|
EM |
HTML5 app using phonegap/cordova |
- |
|
EM |
Preferences |
- |
|
EM |
Using a graphing library |
- |
|
EM |
How to reboot, and why you can’t |
- |
|
EM |
Using a graphing library |
- |
|
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 |
- |
|
EM |
Splash screen |
- |
|
EM |
SQLite demos |
- |
|
EM |
Use Existing Drawables |
- |
|
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 |
- |
|
Todo Sync Client |
Moved to own repo |
||
EM |
Find device’s unique ID |
- |
|
EM |
Make the device vibrate |
- |
|
EM |
Swipe among views |
- |
|
EM |
Demo of Volley networking library |
- |
|
EM |
Graphical window background |
- |