Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Chapter 15 Example project


May 21, 2021 Android SDK Getting started


Table of contents


Chapter 15 Example project

Apps in the Android SDK sample project can perform functions such as various user interface elements, data management, interactions, media, and connection usage instructions. E ven if you don't plan to use some of the specific app types included in the sample during your own development, most of the functionality still applies to other different types of apps. All in all, these sample resources are worth exploring.

1. Install

The first step

To use the Android sample project directly in Eclipse, you first need to make sure you've put it in place correctly. S elect Window in Android SDK Manager, and you'll find different examples in each API-level folder in the list of pulled packages -- select the latest one and expand it. If one of the Samples for SDKs is not already installed, select and install it now.

Chapter 15 Example project

Once the sample project is installed, you can find them directly in Eclipse. Keep in mind that we talked about how to keep Android SDK tools up-to-date with SDK Manager in previous articles in this series of tutorials, so please install them as soon as you currently have updateable content.

2. Create a sample project

The first step

You can create a sample project in Eclipse to view the code in the sample and run it on a physical or virtual device. T his allows us to copy and paste to borrow the algorithms involved in the example, or to interpret and learn to grasp the concept and use it in our own applications in the future. T o create a sample project in Eclipse, select File, New, and then Project. After expanding the Android folder, select Android Sample Project and click Next.

Chapter 15 Example project

Select a create target and click Next.

Chapter 15 Example project

Now you can choose from a list of examples, including early legacy examples, most of which are full-featured applications, including the types of processes we might need in our first application development effort. You can take a slow look at these examples in your spare time, but as an initial contact, let's first select the Notpad example and click Finish.

Chapter 15 Example project

Eclipse uses sample code to create apps in our workspaces, just as we create our own apps. W hen you're done, you should be able to find the Notepad app in your Package Explorer. Expand the app's folder, and we can take a closer look at what's in it.

Chapter 15 Example project

Step two

You can take a look at the individual files contained in the sample project, including Manifest, source doe, and resource files, such as layouts, drawable objects, values, and menus. T he Notepad app is a great resource for beginners to learn about many common feature types on Android platforms. Now let's open the Manifest file and switch to the XML tab.

Don't be intimidated by the complex file structure in the sample file. If you look at these in Eclipse View, especially with Outline View, you'll see that they have the same overall structure as the apps we've developed in previous articles.

Chapter 15 Example project

The source file usually contains a large number of code comments, so if you're not sure what a particular snippe means, you can get answers from the comments. E xpand the elements in Outline View and we'll see notesList Activity listed in detail in the form of the application main launcher Activity. Since this is the primary entry point into the application on the system, we can use it as a good starting point for application source code exploration.

Step three

After spending time browsing other Manifest content, open NoteList Activity under the Notepad app src folder.

As you can see, all source code has good annotations, and not only is the role of this class well explained, but the components of each method are explained. Here's how to look at the onCreate method in Outine View.

Chapter 15 Example project

It's intuitive to see that the code in the onCreate method is only slightly different from the ListActivity we've seen before. W e found that each entry in the list references the same layout, the noteslist item. O pen the res layout folder of the application and find the layout. You can switch between the markup of the layout entry and the graphical view, as we've explained before.

You can explore all the sample applications in this general way, by exploring the various components and logically following the reference relationship to step through the source files.

Step four

You can run these sample apps on a physical device or android virtual device (AVD) in exactly the same way as you would run an application you've developed. L et's clear our minds again: plug in a device or start an AVD, and then go to ConfigurationRuns. S elect Android Application, click the New button and use the Browse button to locate the sample app you want to run. As you can see, the process of running a sample app is no different from running an app you created yourself.

If you use features similar to those in the sample app when developing your application, you can use the sample app content to open and place it next to your own app to use it as a reference to develop your own algorithm. A pplications like the Notepad sample contain a number of very typical processes, such as processing data through TheContent Provider -- something we can see in the NotePadProvider class. Sample apps are designed to show developers how to accomplish specific tasks on the Android platform, so they can be a great help when you follow the instructions in the Android Developer's Guide.

Tip: When we use early legacy examples from the Android SDK, We'll see Eclipse display warnings about discarded code. T his is because the early legacy examples were created for the earlier SDK version. I f you want to use any of the features listed as "not recommended" by Eclipse, first check to see if the new version of the Android SDK offers a more effective alternative to updates.

Step five

Feel free to explore the code content in the SDK sample, many of which have very clear implications. S ometimes, however, the relationship between its content functionality and the sample name may not be directly related. One of the most interesting examples is Api Demos, which you can find in the Notepad app list as described earlier.

Start a sample project with the Api Demos sample, expand it in Package Explorer as described earlier, and then open the src folder. T he app is split into multiple packages, each corresponding to a different feature type. W e can see and learn a lot of practical knowledge from here. O pen one of the packages and see the classes it contains. A s with other sample code, this part of the code also has detailed and explicit comments that help you understand what each class and method actually does. API Demos apps include graphics, animation, media, security, and accessibility.

3. Other ways to use the example

Creating a sample project in Eclipse is the easiest and most practical way to learn, helping you get a thorough understanding of the detative code suggested by the Android SDK. B ut there are other ways to get the most out of the SDK sample code. Y ou can find specific files for each sample project on your computer from the ADT Bundle directory. I n one of the sdk folders, you'll see a folder called samples. E xamples that we've installed at every platform level can be found here, and they all have their own proprietary folders. I n this folder, you'll also see folders that are divided into different sample types. This is how we browse, open, and interact with source code files.

In the Samples section of the Android developer site, you can download and view the various sample codes on the platform. T hese examples are intended to work with Android Studio created with Gradle. You can download the complete project and import it into Android Studio to interact with the content under the IDE or run the app on an Android device.

Summarize

The Android example includes a number of features. A lthough some of the early legacies now seem a bit dated, they are still valuable as learning materials. I f you find that the functionality you're planning to develop already exists in the sample project, you'll save a lot of development time -- just a few changes to what the SDK gives. In the next article in this series, we'll explore how to publish results after application development and testing are over.