Sometimes, the best way to learn how things are done is to look at some code.
To help you get started quickly, the Android SDK includes a variety of sample code and tutorials that illustrate key concepts and techniques of Android application development. For example, the samples show the structure of the manifest file and the use of activities, services, resources, intents, content providers, and permissions. They also show how to add specialized capabilities to your apps, such as Bluetooth and Contacts integration, multiple screens support, Live Wallpaper, and more.
The SDK provides the samples both as source code and as browseable HTML, as described in the sections below. All of the samples included in the SDK are licensed under the Apache 2.0 license, so feel free to use any of the code in your own applications as needed!
Downloading the Sample Code
The SDK sample code is available to you as a set of downloadable SDK components, each of which contains the samples for a specific Android platform version. Once you have installed the SDK, you can download one or more samples component(s) into your SDK environment using the Android SDK and AVD Manager tool, which is pre-installed in the SDK.
To download the samples, launch the Android SDK and AVD Manager tool and select one of the samples components from the Available Packages panel, for example "Samples for SDK API 7". Select Install Selected, verify and accept the download, then select Install Accepted to download the component into your SDK. If you aren't familiar with the Android SDK and AVD Manager and how to launch or use it, please read the Adding SDK Components document.
When the download is complete, you can find the samples sources on your computer in this location:
You can easily create new Android projects with the downloaded samples, modify them if you'd like, and then run them on an emulator or device.
For example, if you are developing in Eclipse with the ADT Plugin, you can
create a project for the "API Demos" sample app by starting a new Android
Project, selecting "Create project from existing source", and then browsing to
samples directory for the platform version you are
If you are not working in Eclipse, you can create a project for the API Demos
sample using the
android tool, by executing this command:
android update project -s -n API Demos -t <target_ID> -p <path>samples/android-<level>/ApiDemos/
Browsing the Sample Code
For your convenience, the SDK provides browseable source code for the latest versions of the samples. You can use your browser to navigate through the structure of each sample and look at the source code in each of its files.
To browse the samples, go to the List of Sample Apps first. From there you can read a short summary of each sample application and what types of concepts, features, or APIs it includes. Then, use the links provided to move through the directories and files of each sample. The browseable source is generated from the same source code that is downloadable through the Android SDK and AVD Manager, as described above.
The browseable samples files are available online, at the Android Developers site only and are not included in the downloadable offline documentation. Note that, although samples for several platform versions are available for download, only the samples for the latest platform version are browseable online.
More Sample Code
If you are looking for more sample code, check out apps-for-android, a collection of open source applications that demonstrate various Android APIs.