== Android Cookbook Examples
This repo contains over a hundred source code examples from the recipes published in
http://androidcookbook.com/[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 <
> shows which projects are set up for which build tools.
=== TL;DR: Build Most Projects with Apache Maven
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!
=== Maven Build: More Details
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 126.96.36.199, 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:188.8.131.52 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
https://guides.github.com/activities/hello-world/[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 build the whole mess with Gradle
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.”
=== Notes for the First Edition of the book
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
=== DELETED PROJECTS
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.
=== List of Projects
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.
.List of projects with description and build tools
| Project | Builds With | Function | Notes
// INSERT TABLE AFTER HERE - github do not allow include:: in files for obv. sec. reasons
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/AboutBoxDemo[AboutBoxDemo] | GS | A re-usable About dialog. | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/AboutInfo[AboutInfo] | EM | Info about device and screen | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/ActionBarCompat[ActionBarCompat] | GS | Simple ActionBar demo(s) | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/AndroidPlot[AndroidPlot] | EM | Graphing | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/AndroidRss[AndroidRss] | EM | RSS Reader | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/AppDownloader[AppDownloader] | MGS | Installs another app | Downloading, package management
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/AutocompleteTextViewContacts[AutocompleteTextViewContacts] | EM | Contact picker with autocomplete | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/AutoUpdater[AutoUpdater] | EM | App can update itself | see AppDownloader
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/BackupManager[BackupManager] | EM | Backups | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/BluetoothDemo[BluetoothDemo] | EM | Bluetooth Connection | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/CalAdder[CalAdder] | EM | Add appointment to Calendar | Calendar via Content Provider
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/CallFaker[CallFaker] | EM | Pretend a call happened, for testing | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/CardDemo[CardDemo] | GS | Demonstation of Card widget. | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/CheckBoxRadioButton[CheckBoxRadioButton] | EM | GUI demo | name says it
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/ContentProviderBookmarks[ContentProviderBookmarks] | EM | List of bookmarks from Bookmarks Content Provider | Browser ContentProvider
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/ContentProviderList[ContentProviderList] | EM | List of ContentProviders | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/ContentProviderSample[ContentProviderSample] | EM | Implement a ContentProvider | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/ContentProviderTest[ContentProviderTest] | EM | Test it | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/CountDownTimerExample[CountDownTimerExample] | EM | Time-based activity | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/CurrentMoodWidget[CurrentMoodWidget] | M | Application Widget | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/CursorLoaderDemo[CursorLoaderDemo] | EM | How to load data properly using CursorLoader | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/CustomMenu[CustomMenu] | EM | Menu | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/CustomSubMenu[CustomSubMenu] | GS | Submenu, created programmatically | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/CustomToast[CustomToast] | EM | Toast | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/DatabaseFromFile[DatabaseFromFile] | EM | Create a database from an SQLite .db file in ‘assets’. | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/DataToCursor[DataToCursor] | EM | Fake up a Cursor from a list of files | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/DialogDemos[DialogDemos] | EM | Dialogs | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/DragDropDemo[DragDropDemo] | E | A simple Drag-and-drop example. | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/DreamsDemo[DreamsDemo] | EM | Demo of “Dreams” (4.x screensaver) | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/EmailTextView[EmailTextView] | EM | Send contents of EditText via email | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/EmailWithAttachments[EmailWithAttachments] | EM | Attach file to email | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/EventListenersDemo[EventListenersDemo] | M | 5 Ways to Wire an Event Listener. | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/FacebookSdk[FacebookSdk] | EM | Access FB API | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/FaceFinder[FaceFinder] | EM | Face locator API in photos | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/FileProviderDemo[FileProviderDemo] | GS | Demo of FileProvider. | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/FilesystemDemos[FilesystemDemos] | MGS | Internal and external files. | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/FloatingActionButtonSnackbarDemo[FloatingActionButtonSnackbarDemo] | GS | Demo of ‘fab’ Floating Abstract Button | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/FontDemo[FontDemo] | EM | Using a custom font | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/FragmentsDemos[FragmentsDemos] | GS | Simple Fragment and List-Detail Fragments in one project | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/Gallery[Gallery] | EM | Simple photo gallery | Deprecated
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/GcmClient[GcmClient] | EM | Google Cloud Messaging - receiver | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/GcmMockServer[GcmMockServer] | EM | Google Cloud Messaging - sender | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/HelloCommandLine[HelloCommandLine] | EM | HelloWorld | SDK
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/HelloEclipse[HelloEclipse] | EM | HelloWorld | Eclipse ADT
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/HelloEspressoTesting[HelloEspressoTesting] | GS | HelloWorld of EspressoTesting | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/HelloGradle[HelloGradle] | GS | Hello World with Gradle but NOT Studio. | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/HelloMaven[HelloMaven] | EM | HelloWorld | Maven, JayWay Plug-in
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/HelloStudioTesting[HelloStudioTesting] | GS | HelloWorld of StudioTesting | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/HelloTestingTarget[HelloTestingTarget] | EM | ADT Testing - subject | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/HelloTestingTestProject[HelloTestingTestProject] | EM | ADT Testing - Tests | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/HomeAppListView[HomeAppListView] | EM | A Home Application | Shows running a program
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/HotContacts[HotContacts] | EM | Add a contact | Shows ContactsContract, content provider
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/IconizedListView[IconizedListView] | EM | List View with Icon | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/IntentsDemo[IntentsDemo] | EM | Intents | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/IpcServiceDemo[IpcServiceDemo] | GS | IPC - Inter-Process Communication, simplified demo. | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/JavaTimeDemo[JavaTimeDemo] | MGS | Demos of java.time date/time API | Actually JSR-310 API; almost same but different package
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/ListViewAdvanced[ListViewAdvanced] | EM | ListView | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/ListViewDemos[ListViewDemos] | EM | ListView | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/ListViewEmpty[ListViewEmpty] | EM | ListView with empty list: don’t show blank screen! | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/ListViewSectionHeader[ListViewSectionHeader] | EM | ListView with section headers | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/LocalBroadcastDemo[LocalBroadcastDemo] | EM | Local Broadcast Receiver | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/MapDemosV2[MapDemosV2] | EM | Google Maps V2 API | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/MarketSearch[MarketSearch] | EM | Search the Play Store | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/MaterialDemos[MaterialDemos] | EM | MaterialDesign demos | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/MediaPlayerDemo[MediaPlayerDemo] | EM | Play media | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/MediaPlayerInteractive[MediaPlayerInteractive] | E | Media Player demo from Marco Dinacci, thanks. | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/MediaRecorderDemo[MediaRecorderDemo] | EM | Simple sound recorder | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/NdkDemo[NdkDemo] | EM | Native Development Kit | C Code
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/NotificationDemo[NotificationDemo] | EM | Notifications | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/NumberPickers[NumberPickers] | EM | UI for picking numbers | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/OAuth2Demo[OAuth2Demo] | GS | Demo of OAuth2 to Google Tasks. | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/OpenGlDemo[OpenGlDemo] | EM | OpenGL graphics | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/OrientationChanges[OrientationChanges] | EM | Rotation | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/OSMIntro[OSMIntro] | EM | OpenStreetMap API | (free alternative to Google Maps)
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/PdfShare[PdfShare] | EM | Create and share a PDF | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/PermissionRequest[PermissionRequest] | G | Shows asking for permissions at runtime, i.e,. “the new way” | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/PhoneGapDemo[PhoneGapDemo] | EM | HTML5 app using phonegap/cordova | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/PinchAndZoom[PinchAndZoom] | GS | Pinch-and-zoom graphics demo | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/PreferencesDemo[PreferencesDemo] | EM | Preferences | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/RangeGraphDemo[RangeGraphDemo] | EM | Using a graphing library | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/Rebooter[Rebooter] | EM | How to reboot, and why you can’t | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/RecyclerViewDemo[RecyclerViewDemo] | GS | Demo of RecyclerView, the faster ListView. | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/RGraphDemo[RGraphDemo] | EM | Using a graphing library | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/RuntimeLog[RuntimeLog] | EM | A simpe runtime log facility | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/SecondScreen[SecondScreen] | M | Trivial example of one Activity (screen) starting another. | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/SendSMS[SendSMS] | EM | Sends an SMS | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/ShareActionProviderDemo[ShareActionProviderDemo] | EM | ICS “Share” action | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/SimpleAccountAndSyncDemo[SimpleAccountAndSyncDemo] | EM | User account on device | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/SimpleCalendar[SimpleCalendar] | EM | Calendar | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/SimpleJumper[SimpleJumper] | M | Flixel-based gaming demo | This example still needs to be re-created.
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/SimplePool[SimplePool] | EM | Graphics | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/SimpleTorchLight[SimpleTorchLight] | EM | Flashlight/torch from camera flash | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/SlidingDrawer-Topdown[SlidingDrawer-Topdown] | EM | The “Sliding Drawer” view | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/SnackBarDemo[SnackBarDemo] | EM | Snackbar (bottom message text) demo | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/SoapDemo[SoapDemo] | EM | XML SOAP Web Service | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/SoundRec[SoundRec] | EM | Sound Recorder | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/Speaker[Speaker] | EM | Text To Speech (TTS) | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/SpinnerDemo[SpinnerDemo] | EM | Drop-down Spinner Demos | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/SpinningCubeDemo[SpinningCubeDemo] | E | OpenGL Spinning Cube, from Marco Dinacci | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/SplashDialog[SplashDialog] | EM | Splash screen | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/SQLiteDemos[SQLiteDemos] | EM | SQLite demos | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/StandardDrawables[StandardDrawables] | EM | Use Existing Drawables | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/StaticFileRead[StaticFileRead] | EM | StaticFileRead - read a file from the application | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/TabSwipeDemo[TabSwipeDemo] | EM | Tabbable and swipeable layout | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/TeleCorder[TeleCorder] | EM | Telephone call recorder (in progress!) | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/TelephonyManager[TelephonyManager] | EM | Displays phone info | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/Tipster[Tipster] | EM | Full app: compute and show restaurant bill with tip | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/UniqueId[UniqueId] | EM | Find device’s unique ID | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/Vibrate[Vibrate] | EM | Make the device vibrate | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/ViewPagerDemo[ViewPagerDemo] | EM | Swipe among views | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/VolleyDemo[VolleyDemo] | EM | Demo of Volley networking library | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/WheelPickerDemo[WheelPickerDemo] | GS | Apple-style Wheel Picker Demo | -
| link:https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/WindowBackgroundDemo[WindowBackgroundDemo] | EM | Graphical window background | -
Collected code examples from the O'Reilly Android Cookbook. See README below! ...
Awesome rm cookbook examples for android ...
Collected Examples from the O'Reilly Android Cookbook ...
Collected code examples from the O'Reilly Android Cookbook ...
Collected code examples from the O'Reilly Android Cookbook ...