In this series, we have been exploring the basics of Android development. We have looked at what you need to know when building the elements in Android apps. In this tutorial, however, you will see that the Android platform provides resources you can learn from. The samples of the Android SDK include complete applications in which you can see functioning examples of typical Android application components. You can use the samples in more than one way, including directly from within Eclipse.
The Android SDK samples include apps performing various types of functionality, including a variety of user interface elements, data management, interaction, demonstrations of using media and connectivity. Even if you do not wish to build the specific types of app you see in the samples, many of them include functionality that is common to many different app types, so the resources are well worth exploring.
To use the Android samples directly from within Eclipse, you first need to make sure you have them installed. Select Window then Android SDK Manager. In the package list, you will find the samples inside each API level folder - select the most recent one and expand it. If Samples for SDK is not already installed, select it and install it now.
Once you have the samples installed, you can use them directly from within Eclipse. Remember that we discovered near the beginning of the series how you can regularly keep your Android SDK tools up to date using the SDK Manager, so you may wish to do this at the same time if there are updates ready.
2. Creating Sample Projects
You can create a sample project in Eclipse, letting you see the code within the samples as well as running them on actual or virtual devices. This lets you make use of the algorithms within the samples by copying and pasting from them, or just learn from them, using them as a reference for your own apps. To create a sample project in Eclipse, choose File, New then Project. Expand the Android folder, choose Android Sample Project and click Next.
Choose a build target and click Next.
Now you can choose from the list of samples, including the legacy samples, many of which are fully functional apps containing the types of processing your initial apps may need. You can explore the samples in your own time, but to start with, choose the Notepad sample and click Finish.
Eclipse will build the app in your workspace as it does with your own app. You should see the Notepad app now in your Package Explorer. Expand the folder for the app so that you can explore its contents.
You can take your time looking at the various files in the sample project, including the Manifest, the source code, and the resource files, such as the layouts, drawables, values, and menus. The Notepad app is a great resource for finding out about lots of common types of functionality on Android. Start by opening the Manifest file and switching to the XML tab.
Don't be intimidated by the complexity of the files in the samples. If you explore them using the Eclipse Views, the Outline View in particular, you will see that they have the same overall structure as the simple app we created earlier in this series.
The source files typically include lots of notes as code comments so there is plenty of explanation if you are unsure what a particular code snippet is meant to do. Expand the elements in the Outline View and you will see that the NotesList Activity is listed as main launcher Activity for the app. Since this is the main entry point for the app on the system, it makes a good place to start exploring the app source code.
Once you have taken some time to look through the other Manifest contents, open the NoteList Activity class in the src folder of the Notepad app.
As you can see, the source code is well commented, outlining the responsibility of the class as well as explaining the task of each of its methods or parts of a method. Take a look at the onCreate method in the Outline View.
As you will notice straight away, the code in the onCreate method is slightly different from what we have seen before as this is a ListActivity. You will see a reference to a layout for each item in the list, noteslist_item. Locate the layout in the app's res layout folder and open it. You can switch between the markup and graphical views of the layout item as we saw earlier.
You can use this general approach to exploring the sample apps, by navigating between the various elements, following references between the source files in a logical way.
You can run sample apps on physical devices or AVDs as you would with an app you created yourself. To do so, connect a device or launch an AVD, and browse to Run Configurations. Select Android Application, click the New button, and locate the sample app using the Browse button. As you can see, the process of running a sample app is exactly the same as running your own apps.
If you are developing an app with similar functionality to what is in one of the sample apps, you can keep the sample app open alongside your own app, referring to it as you develop your own algorithms. Apps such as the Notepad sample include handy overviews of typical processes such as handling data via a Content Provider, which you can see in the NotePadProvider class. The samples are designed to showcase how you can implement certain tasks on the Android platform and can therefore be extremely useful as practical demonstrations of what you learn in the Developer Guide.
Tip: When you use the legacy samples in the Android SDK, you will see Eclipse displaying warning messages regarding deprecated code. This happens because the legacy samples were built for previous versions of the SDK. If you plan on using any functionality listed as deprecated in Eclipse, try to find out what the recommended alternatives are for more recent versions of the Android SDK.
Feel free to further explore the SDK samples. The content of many samples should be self-explanatory. However, sometimes the contained functionality is not immediately obvious from the sample's name. One sample worth checking out is the API Demos sample. You can find it in the list as we did with the Notepad app.
Start a sample project with the API Demos sample, expand it in the Package Explorer as we did earlier, and open the src folder. The application is divided into packages for various types of functionality. There is a lot of processing to see and learn from. Open a package to explore the classes it contains. As with the other sample code, the code is well commented to help you understand what each class and method does. The API Demos application includes graphics, animation, media, security, and accessibility functions.
3. Other Ways to Use Samples
Creating sample projects in Eclipse is the easiest and most useful way to learn from the Android SDK sample code. However, there are other ways to use the samples included in the SDK. You can find the files for each sample project in the ADT Bundle directory on your computer. Inside the sdk folder, you will see a folder named samples. Each platform level for which you have installed samples will be listed here with its own dedicated folder. Inside this folder you will see various folders for sample categories. You can browse, open, and interact with the source code files in this way as well.
The Android Developers site Samples section also lets you download and view sample code for the platform. These samples are built to use with Android Studio as Gradle builds. You can download complete projects and import them into Android Studio, letting you interact with their content in the IDE as well as running the apps on an Android device.
The Android samples include a lot of functionality. Although some of the legacy content is a little out of date, there is still plenty you can learn from them. If you are trying something that one of the sample projects already does, you can save yourself a lot of development time simply by looking at one possible way of achieving what you are working towards. In the next part of the series we will run through the steps you need to take in order to publish your apps once you have developed and tested them.