This repo is among the largest collections of Android code samples anywhere; it contains around a hundred and fifty compilable source code examples from 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 some 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 There are two styles of projects:
-
Eclipse projects with the newer Eclipse Andmore;
-
Android Studio projects (most of them 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 also have configuration files to allow them to be opened in Android Studio.
Many of the Eclipse projects have been Mavenized; a few have not.
Most of the Gradle projects will also build with Android Studio; there may be a few that will not.
The table List of Projects shows which projects are set up for which build tools.
Prerequisites: Java 7 or 8 SDK, Android SDK including "Google Maven 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 # also needed
-
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!
If you get Eclipse-Maven projects failing with this message: org.codehaus.plexus.archiver.jar.Manifest.write(java.io.PrintWriter)
then you need to update your "M2E Connector for Android" to at least 0.17.2.20060614
About two thirds of these projects were initially developed using Eclipse ADT, and migrated to Maven for automatic building. Obviously Google has spoken and we will eventually have to migrate kicking and screaming to Android Studio and Gradle, but for now, we are keeping those projects in "bilingual" or "either-IDE mode".
If you import 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 Github repository. If a project won’t open, or won’t build, we’d prefer that you fix it and sent a pull request. Bugs and suggestions about code samples can 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/0636920038092.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
.
JpsTrack and the ToDo applications have graduated to their own github projects, up one level from here on Github.
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. |
- |
|
EMG |
Info about device and screen |
- |
|
GS |
Simple ActionBar demo(s) |
- |
|
EMG |
Add a contact |
Shows ContactsContract, content provider |
|
GS |
Using AdMob with interstitial (inter-Activity) ads. |
- |
|
EMG |
Graphing |
- |
|
EMG |
RSS Reader |
Works under Maven; Eclipse fails w/ Maven Configuration Problem |
|
MGS |
Installs another app |
Downloading, package management |
|
GS |
Compilable example of the Application Singleton pattern recipe. |
- |
|
EMG |
Contact picker with autocomplete |
- |
|
EMG |
App can update itself |
see AppDownloader |
|
EMG |
Use Android Backup scheme. |
- |
|
EMG |
Bluetooth Connection |
- |
|
EMG |
Add appointment to Calendar |
Calendar via Content Provider |
|
EMG |
Pretend a call happened, for testing |
- |
|
EG |
Intercept an incoming phone call. |
- |
|
EG |
Intercept outgoing phone calls. |
Compiles and runs, but doesn’t intercept the call. |
|
GS |
CameraIntent - get the camera to take a picture, by startActivityForResult() |
- |
|
GS |
Demonstation of Card widget. |
- |
|
EMG |
GUI demo |
name says it |
|
EMG |
List of bookmarks from Bookmarks Content Provider |
Browser ContentProvider |
|
EMG |
List of ContentProviders |
- |
|
EMG |
Implement a ContentProvider |
- |
|
EMG |
Test it |
- |
|
EMG |
Time-based activity |
- |
|
EMG |
Application Widget |
- |
|
EMG |
How to load data properly using CursorLoader |
- |
|
EMG |
Menu |
- |
|
GS |
Submenu, created programmatically |
- |
|
EMG |
Toast |
- |
|
EMG |
Create a database from an SQLite .db file in 'assets'. |
- |
|
EMG |
Fake up a Cursor from a list of files |
- |
|
EMG |
Dialogs |
- |
|
EG |
A simple Drag-and-drop example. |
- |
|
EMG |
Demo of "Dreams" (4.x screensaver) |
- |
|
EMG |
Send contents of EditText via email |
- |
|
EMG |
Attach file to email |
- |
|
EMG |
Java + JavaScript Epoch Calendar |
- |
|
M |
5 Ways to Wire an Event Listener. |
- |
|
EMG |
Access FB API |
- |
|
EMG |
Face locator API in photos |
- |
|
GS |
Demo of FileProvider. |
- |
|
MGS |
Internal and external files. |
- |
|
EG |
Find Me X |
IllegalArgumentException: Incomplete location object, missing timestamp or accuracy, Progress:334 |
|
GS |
Demo of 'fab' Floating Abstract Button |
- |
|
EMG |
Using a custom font |
You need to provide the font file! Example uses Iceberg font… |
|
GS |
Simple Fragment and List-Detail Fragments in one project |
- |
|
EMG |
Simple Photo Gallery example. |
Deprecated |
|
EMG |
Google Cloud Messaging - receiver |
- |
|
EMG |
Google Cloud Messaging - sender |
- |
|
GS |
Haptic Feedback recipe. |
- |
|
EMAG |
HelloWorld |
SDK |
|
EMG |
HelloWorld |
Eclipse ADT |
|
GS |
HelloWorld of EspressoTesting |
- |
|
GS |
Hello World with Gradle but NOT Studio. |
- |
|
EMG |
HelloWorld using Maven with android-maven-plugin. |
- |
|
GS |
HelloWorld of StudioTesting |
- |
|
EMAG |
ADT Testing - subject |
- |
|
EMAG |
ADT Testing - Tests |
- |
|
EMG |
A Home Application |
Shows running a program |
|
EMG |
List View with Icon |
- |
|
EMG |
Intents |
- |
|
GS |
IPC - Inter-Process Communication, simplified demo. |
- |
|
EMG |
Getting data from Java to JavaScript and back. |
- |
|
MGS |
Demos of java.time date/time API |
Actually JSR-310 API; almost same but different package |
|
EG |
Simple example of JSON parsing using built-in JSONObject. |
- |
|
G |
Layout Border Demo Project Source for Android |
- |
|
EMG |
ListView |
- |
|
EMGS |
ListView |
- |
|
EMGS |
ListView with empty list: don’t show blank screen! |
- |
|
EMG |
ListView with section headers |
- |
|
EMG |
Local Broadcast Receiver |
- |
|
EMG |
This is the Google API Maps V2 Demo. |
- |
|
GS |
Adding a Google Maps V2 MapActivity to a project |
You must ask for permission from Google (get and provide your own Google API key). |
|
EMG |
Search the Play Store |
- |
|
EMG |
MaterialDesign demos |
- |
|
EMG |
Play media |
- |
|
EG |
Media Player demo from Marco Dinacci, thanks. |
- |
|
EMG |
Simple sound recorder |
- |
|
EMG |
Menu Action Handling |
- |
|
EMG |
Native Development Kit |
C Code |
|
EMG |
Notifications |
- |
|
EMG |
UI for picking numbers |
- |
|
GS |
Demo of OAuth2 to Google Tasks. |
- |
|
EMG |
OpenGL graphics |
- |
|
EMG |
Rotation |
- |
|
EMG |
Basic OSM map app demo. |
Builds with Maven, not with Eclipse. Maybe m2e aar support, isn’t, yet. |
|
EMG |
OSM Map with a map marker overlay |
Compiles, runs, doesn’t load map tiles; needs upgrading to osmdroid-android 5.5 |
|
EMG |
OSM Map with touch event handling. |
Builds with Maven, not with Eclipse. Maybe m2e aar support, isn’t, yet. |
|
EMG |
Create and share a PDF |
- |
|
G |
Shows asking for permissions at runtime, i.e,. "the new way" |
- |
|
EMG |
HTML5 app using phonegap/cordova |
- |
|
GS |
Pinch-and-zoom graphics demo |
- |
|
EMG |
Preferences |
- |
|
GS |
Demo of a pop-up indefinite progress dialog. |
- |
|
EMG |
Using a graphing library |
- |
|
EMG |
How to reboot, and why you can’t |
- |
|
EG |
= RecipeList - use an AsyncTask to download data over the 'net via HTTP. |
- |
|
GS |
Demo of RecyclerView, the faster ListView. |
- |
|
EM |
This folder is created automatically by Eclipse and can be ignored. |
- |
|
EMG |
Using a graphing library |
- |
|
EMG |
A simpe runtime log facility |
- |
|
GS |
Scan barcode using ZXing app via Intent |
- |
|
M |
Trivial example of one Activity (screen) starting another. |
- |
|
EMG |
Sends an SMS |
- |
|
GS |
Use Accellerometer to detect if the user has shaken the device. |
- |
|
GS |
Code for r2051, Checking Whether a Device Is Facing Up or Down |
- |
|
EMG |
ICS "Share" action |
- |
|
EG |
Run a Unix/Linux command via Runtime.exec, capture the output. |
- |
|
EMG |
User account on device |
- |
|
EMG |
Calendar |
- |
|
EG |
Common code for flixel-gdx-based gaming demo. |
Game starts, can move left-right only, no scenery. Fails on Intel phone/emulator |
|
EG |
Start of a pool/billiards game using AndEngine. |
- |
|
EMG |
Simple Torchlight/Flashlight example. |
(Most devices now have a built-in control for this) |
|
EMG |
The main code is from the "Sliding Drawer - Top Down" recipe |
- |
|
EG |
Receive an incoming SMS. |
- |
|
EMG |
Snackbar (bottom message text) demo |
- |
|
EMG |
XML SOAP Web Service |
- |
|
GS |
Change the Enter key to "Next" on one-line text fields. |
- |
|
EMG |
Sound Recorder |
- |
|
EMG |
Text To Speech (TTS) |
- |
|
GS |
Speech Recognizer. |
- |
|
EMG |
Drop-down Spinner Demos |
- |
|
EG |
OpenGL Spinning Cube, from Marco Dinacci |
- |
|
EMG |
Splash screen |
- |
|
EMG |
SQLite demos |
- |
|
EMG |
Use Existing Drawables |
- |
|
EMG |
StaticFileRead - read a file from the application |
- |
|
G |
Demos for "Nuances of Strings.xml" recipe. |
- |
|
EMG |
Tabbable and swipeable layout |
- |
|
EMG |
Telephone call recorder (in progress!) |
- |
|
EMG |
Displays phone info |
- |
|
EMGS |
Full app: compute and show restaurant bill with tip |
- |
|
EMG |
Find device’s unique ID |
- |
|
EMG |
Make the device vibrate |
- |
|
EMG |
Swipe among views |
- |
|
EMG |
Demo of Volley networking library |
- |
|
GS |
Apple-style Wheel Picker Demo |
- |
|
EMG |
Graphical window background |
- |