Next   Previous section   Next section

Mobile Phone Handheld Hardware Hardware Rick Rogers John Lombardo O'Reilly Media, Inc. O'Reilly Media Android Application Development, 1st Edition

6.2. Finding the Source to an Interesting Example

The ApiDemos application has a lot of interesting examples that will help you learn how to program an Android application. However, it's not entirely obvious how to find the source to any particular screen. The following procedure will help you find the source to any ApiDemo you're interested in. To understand the process, we'll trace a couple of demos: the "App/Activity/Custom Title" and the "Text/Linkify" examples.

6.2.1. Custom Title Demo

This technique works when the ApiDemos application stores information about the demo in the res/strings.xml resource file:

  1. After starting the ApiDemos application, find the particular demo by clicking on the menu, and remember the path you took through the menu system. In this case, you click on App, then Activity, and finally Custom Title.

  2. Open the res/values/strings.xml file in a text editor such as Eclipse (actually, any text editor that can do regular expression searches should work fine). Carry out a regular expression search (Ctrl-F Ctrl-X in Eclipse) for each of the menu words from step 1. Use the regular expression ".*" to separate the words. Thus, the search term in our example is App.*Activity.*Custom.*Title. The search should return zero or one result.

    If you don't find any results, use the procedure in the following section of this chapter. Otherwise, the single result should be the contents of a string element. The value of the name attribute of that string element is our search term for the next step. For our example, this is activity_custom_title.

  3. Open the AndroidManifest.xml file and search it for the string you found in the previous step: activity_custom_title. The search should return only one result, which should be part of the the value of the android:label attribute within an activity element. That activity element should also contain an android:name attribute. The value of this attribute contains the path to the Activity class that implements the demo. In our example it's .app.CustomTitle. This translates to the CustomTitle.java files in the app subdirectory of the source tree.

In the end, therefore, the source for the App Activity Custom Title menu item can be found in samples/ApiDemos/src/com/example/android/apis/app/CustomTitle.java.

6.2.2. Linkify Demo

This technique should work for demos that you can't find with the previous method. If the ApiDemos application doesn't store information about the demo in res/strings.xml, it gets its information directly from AndroidManifest.xml—and so will we.

  1. After starting the ApiDemos application, find the particular demo through clicking on the menu, and remember the path you took through the menu system. In this case, you click on Text and then Linkify.

  2. Open the AndroidManifest.xml file and search for the menu elements as in the previous example. But this time the menu elements must be separated by slashes instead of ".*" regular expressions. So in this case, search for the text Text/Linkify (it doesn't have to be a regular expression search).

    The search should return only one result, which should be part of the the value of the android:label attribute within an activity element. That element should also contain an android:name attribute. The value of this attribute contains the path to the Activity class that implements the demo. In our example, the path is .text.Link. This translates to the Link.java file within the text subdirectory of the source tree.

So in this example, the source for the Text Linkify menu item can be found in samples/ApiDemos/src/com/example/android/apis/text/Linkify.java.

          
    Next   Previous section   Next section