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).
The started-in-Eclipse projects also have configuration files to allow them to be opened in Android Studio.
During the runup to the second edition of the book, AndMore appeared to be a viable project to maintain Android Eclipse development. However, over time, it has become clear that AndMore has not attracted enough developer talent to remain viable. Accordingly, the newest projects here are Android-Studio-Only.
The table List of Projects shows which projects are set up for which build tools.
*Note that, since Gradle and the Gradle Android Plugin need to be updated just about every time your run Android Studio, there is no attempt made to keep things up-to-date here; just let Studio do its upgrade thing for you and all will be well. They promise.
Prerequisites: Java 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. The Author names have recently been added; if I have missed or mis-attributed any, please let me know and I will happily correct them. You (and I) know who you are.
Project | Author | Builds With | Function | Notes |
---|---|---|---|---|
Ian Darwin |
GS |
A re-usable About dialog. |
- |
|
Ian Darwin |
EMGS |
Info about device and screen |
- |
|
Ian Darwin |
GS |
Simple ActionBar demo(s) |
- |
|
Ian Darwin |
EMG |
Add a contact |
Shows ContactsContract, content provider |
|
Ian Darwin |
GS |
Using AdMob with interstitial (inter-Activity) ads. |
- |
|
Ian Darwin |
EMGS |
Ashwini’s Google Analytics for Android Demo, slightly updated. |
- |
|
Rachee Singh |
EMG |
Graphing |
- |
|
Wagied Davids |
EMG |
RSS Reader |
Works under Maven; Eclipse fails w/ Maven Configuration Problem |
|
Ian Darwin |
MGS |
Installs another app |
Downloading, package management |
|
Ian Darwin |
GS |
Shows App Shortcuts, which appear with long-press on app’s launcher icon. |
- |
|
Ian Darwin |
GS |
Compilable example of the Application Singleton pattern recipe. |
- |
|
Ian Darwin |
EMG |
Contact picker with autocomplete |
- |
|
Ian Darwin |
EMG |
App can update itself |
see AppDownloader |
|
Ian Darwin |
EMG |
Use Android Backup scheme. |
- |
|
Ian Darwin |
EMG |
Bluetooth Connection |
- |
|
Ian Darwin |
EMG |
Add appointment to Calendar |
Calendar via Content Provider |
|
Ian Darwin |
EMG |
Pretend a call happened, for testing |
- |
|
Johan Pelgrim |
EGS |
Intercept an incoming phone call. |
- |
|
Johan Pelgrim |
EG |
Intercept outgoing phone calls. |
Compiles and runs, but doesn’t intercept the call. |
|
Ian Darwin |
GS |
CameraIntent - get the camera to take a picture, by startActivityForResult() |
- |
|
Ian Darwin |
GS |
Demonstation of Card widget. |
- |
|
Ian Darwin |
EMG |
GUI demo |
name says it |
|
Ian Darwin |
EMG |
List of bookmarks from Bookmarks Content Provider |
Browser ContentProvider |
|
Ian Darwin |
EMG |
List of ContentProviders |
- |
|
Ashwini Shahapurkar |
EMG |
Implement a ContentProvider |
- |
|
Ian Darwin |
EMG |
Test it |
- |
|
Wagied Davids |
EMG |
Time-based activity |
- |
|
Ian Darwin |
EMG |
Application Widget |
- |
|
Ian Darwin |
EMG |
How to load data properly using CursorLoader |
- |
|
Ian Darwin |
EMG |
Menu |
- |
|
Ian Darwin |
GS |
Submenu, created programmatically |
- |
|
Rachee Singh |
EMG |
Toast |
- |
|
Ian Darwin |
EMG |
Create a database from an SQLite .db file in 'assets'. |
- |
|
Ian Darwin |
EMG |
Fake up a Cursor from a list of files |
- |
|
Ian Darwin |
GS |
Example of various DateFormat usages. |
- |
|
Ian Darwin |
EMG |
Dialogs |
- |
|
Ian Darwin |
EG |
A simple Drag-and-drop example. |
- |
|
Ian Darwin |
EMG |
Demo of "Dreams" (4.x screensaver) |
- |
|
Wagied Davids |
EMG |
Send contents of EditText via email |
- |
|
Marco Dinacci |
EMG |
Attach file to email |
- |
|
Wagied Davids |
EMG |
Java + JavaScript Epoch Calendar |
- |
|
Daniel Fowler |
M |
5 Ways to Wire an Event Listener. |
- |
|
Ian Darwin |
EMG |
Access FB API |
- |
|
Wagied Davids |
EMG |
Face locator API in photos |
- |
|
Ian Darwin |
GS |
Demo of FileProvider. |
- |
|
Ian Darwin |
MGS |
Internal and external files. |
- |
|
Ian Darwin |
EG |
Find Me X |
IllegalArgumentException: Incomplete location object, missing timestamp or accuracy, Progress:334 |
|
Ian Darwin |
GS |
Demo of 'fab' Floating Abstract Button |
- |
|
Ian Darwin |
EMG |
Using a custom font |
You need to provide the font file! Example uses Iceberg font… |
|
Ian Darwin |
GS |
Simple Fragment and List-Detail Fragments in one project |
- |
|
Ian Darwin |
EMG |
Simple Photo Gallery example. |
Deprecated |
|
Ian Darwin |
EMG |
Google Cloud Messaging - receiver |
- |
|
Ian Darwin |
EMG |
Google Cloud Messaging - sender |
- |
|
Ian Darwin |
EG |
A program that shows the Storage Facility to prompt for all files. |
- |
|
Ian Darwin |
GS |
Haptic Feedback recipe. |
- |
|
Ian Darwin |
EMAG |
HelloWorld |
SDK |
|
Ian Darwin |
EMG |
HelloWorld |
Eclipse ADT |
|
Ian Darwin |
GS |
HelloWorld of EspressoTesting |
- |
|
Ian Darwin |
GS |
Hello World with Gradle but NOT Studio. |
- |
|
Ian Darwin |
EMG |
HelloWorld using Maven with android-maven-plugin. |
- |
|
Ian Darwin |
GS |
HelloWorld of StudioTesting |
- |
|
Adrián Santalla |
EMAG |
ADT Testing - subject |
- |
|
Ian Darwin |
EMAG |
ADT Testing - Tests |
- |
|
Ian Darwin |
EMG |
A Home Application |
Shows running a program |
|
Ian Darwin |
EMG |
List View with Icon |
- |
|
Ian Darwin |
EMG |
Intents |
- |
|
Ian Darwin |
GS |
IPC - Inter-Process Communication, simplified demo (2 versions, alas). |
- |
|
Rupesh Chavan |
GS |
IPC - Inter-Process Communication, simplified demo (2 versions, alas). |
- |
|
Ian Darwin |
EMG |
Getting data from Java to JavaScript and back. |
- |
|
Ian Darwin |
MGS |
Demos of java.time date/time API |
Actually JSR-310 API; almost same but different package |
|
Ian Darwin |
EG |
Simple example of JSON parsing using built-in JSONObject. |
- |
|
Ian Darwin |
GS |
Kotlin version of Volley demo |
- |
|
Ian Darwin |
G |
Layout Border Demo Project Source for Android |
- |
|
Marco Dinacci |
EMG |
ListView |
- |
|
Ian Darwin |
EMGS |
ListView |
- |
|
Rachee Singh |
EMGS |
ListView with empty list: don’t show blank screen! |
- |
|
Ian Darwin |
EMG |
ListView with section headers |
- |
|
Ian Darwin |
EMG |
Local Broadcast Receiver |
- |
|
Ian Darwin |
EMG |
This is the Google API Maps V2 Demo. |
- |
|
Ian Darwin |
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). |
|
Ian Darwin |
EMG |
Search the Play Store |
- |
|
Ian Darwin |
EMG |
MaterialDesign demos |
- |
|
Ian Darwin |
EMG |
Play media |
- |
|
Ian Darwin |
EG |
Media Player demo from Marco Dinacci, thanks. |
- |
|
Marco Dinacci |
EMG |
Simple sound recorder |
- |
|
Ian Darwin |
EMG |
Menu Action Handling |
- |
|
Ian Darwin |
EMG |
Native Development Kit |
C Code |
|
Ian Darwin |
EMG |
Notifications |
- |
|
Ian Darwin |
EMG |
UI for picking numbers |
- |
|
Ian Darwin |
GS |
Demo of OAuth2 to Google Tasks. |
- |
|
Marco Dinacci |
EMG |
OpenGL graphics |
- |
|
Wagied Davids |
EMG |
Rotation |
- |
|
Rachee Singh |
EMG |
Basic OSM map app demo. |
Builds with Maven, not with Eclipse. Maybe m2e aar support, isn’t, yet. |
|
Ian Darwin |
EMG |
OSM Map with a map marker overlay |
Compiles, runs, doesn’t load map tiles; needs upgrading to osmdroid-android 5.5 |
|
Ian Darwin |
EMG |
OSM Map with touch event handling. |
Builds with Maven, not with Eclipse. Maybe m2e aar support, isn’t, yet. |
|
Ian Darwin |
EMG |
Create and share a PDF |
- |
|
Ian Darwin |
G |
Shows asking for permissions at runtime, i.e,. "the new way" |
- |
|
Shraddha Shravagi |
EMG |
HTML5 app using phonegap/cordova |
- |
|
Ian Darwin |
GS |
Pinch-and-zoom graphics demo |
- |
|
Ian Darwin |
EMG |
Preferences |
- |
|
Ian Darwin |
GS |
Demo of a pop-up indefinite progress dialog. |
- |
|
Ian Darwin |
EMG |
Using a graphing library |
- |
|
Ian Darwin |
EGS |
Show use of 5-star RatingBar as used in rankings, surveys, &c. |
- |
|
Ian Darwin |
EMG |
How to reboot, and why you can’t |
- |
|
Ian Darwin |
EMGS |
= RecipeList - use an AsyncTask to download data over the 'net via HTTP. |
- |
|
Ian Darwin |
GS |
Demo of RecyclerView, the faster ListView. |
- |
|
Ian Darwin |
EM |
This folder is created automatically by Eclipse and can be ignored. |
- |
|
Wagied Davids |
EMG |
Using a graphing library |
- |
|
Ian Darwin |
GS |
Demo of Room, the official Android ORM. |
- |
|
Ian Darwin |
EMG |
A simpe runtime log facility |
- |
|
Ian Darwin |
GS |
Scan barcode using ZXing app via Intent |
- |
|
Ian Darwin |
M |
Trivial example of one Activity (screen) starting another. |
- |
|
Colin Wilcox |
EMG |
Sends an SMS |
- |
|
Thomas Manthey |
GS |
Use Accellerometer to detect if the user has shaken the device. |
- |
|
Rachee Singh |
GS |
Code for r2051, Checking Whether a Device Is Facing Up or Down |
- |
|
Ian Darwin |
EMG |
ICS "Share" action |
- |
|
Ian Darwin |
EG |
Run a Unix/Linux command via Runtime.exec, capture the output. |
- |
|
Ian Darwin |
EMG |
User account on device |
- |
|
Ian Darwin |
EMG |
Calendar |
- |
|
Ian Darwin |
EMGS |
Trivial Dialer - launches Dialer app via Intent |
- |
|
Wagied Davids |
EG |
Common code for flixel-gdx-based gaming demo. |
Game starts, can move left-right only, no scenery. Fails on Intel phone/emulator |
|
Wagied Davids |
EG |
Start of a pool/billiards game using AndEngine. |
- |
|
Saketkumar Srivastav |
EMG |
Simple Torchlight/Flashlight example. |
(Most devices now have a built-in control for this) |
|
Wagied Davids |
EMG |
The main code is from the "Sliding Drawer - Top Down" recipe |
- |
|
Ian Darwin |
EG |
Receive an incoming SMS. |
- |
|
Ian Darwin |
EMG |
Snackbar (bottom message text) demo |
- |
|
Ian Darwin |
EMG |
XML SOAP Web Service |
- |
|
Jonathan Fuerth |
GS |
Change the Enter key to "Next" on one-line text fields. |
- |
|
Ian Darwin |
EMG |
Sound Recorder |
- |
|
Ian Darwin |
EMG |
Text To Speech (TTS) |
- |
|
Ian Darwin |
GS |
Speech Recognizer. |
- |
|
Ian Darwin |
EMG |
Drop-down Spinner Demos |
- |
|
Marco Dinacci |
EG |
OpenGL Spinning Cube, from Marco Dinacci |
- |
|
Rachee Singh |
EMG |
Splash screen |
- |
|
Ian Darwin |
EMG |
SQLite demos |
- |
|
Ian Darwin |
EMG |
Use Existing Drawables |
- |
|
Ian Darwin |
EMG |
StaticFileRead - read a file from the application |
- |
|
Daniel Fowler |
G |
Demos for "Nuances of Strings.xml" recipe. |
- |
|
Ian Darwin |
EMGS |
TabHost Demo: Demo of the TabHost widget, in a Dialog. |
- |
|
Ian Darwin |
EMG |
Tabbable and swipeable layout |
- |
|
Ian Darwin |
EMG |
Telephone call recorder (in progress!) |
- |
|
Pratik Rupwal |
EMG |
Displays phone info |
- |
|
Sunit Katkar |
EMGS |
Full app: compute and show restaurant bill with tip |
- |
|
Ian Darwin |
EMG |
Find device’s unique ID |
- |
|
Rachee Singh |
EMG |
Make the device vibrate |
- |
|
Ian Darwin |
EMG |
Swipe among views |
- |
|
Ian Darwin |
EMGS |
Demo of Volley networking library |
- |
|
Wagied Davids |
GS |
Apple-style Wheel Picker Demo |
- |
|
Ian Darwin |
EMG |
Graphical window background |
- |