1. Application Setup in the Manifest File
Like every other Android application, the best place to get a sense of how the
application is strung together is the application’s AndroidManifest.xml file. Let’s take a look at
part of the AndroidManifest.xml file
for ApiDemos, near the beginning of the file:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.apis">
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<application android:name="ApiDemosApplication"
android:label="@string/activity_sample_code"
android:icon="@drawable/app_sample_code" >
<uses-library android:name="com.google.android.maps" />
<activity android:name="ApiDemos">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Here are some of the highlights of the code:
The ApiDemosApplication class,
found in the top-level directory of the source code, extends the Application class and has two methods: onCreate and onTerminate. The onCreate method executes before any activities
start. Application-level global variables should be defined and
initialized by the onCreate method.
It’s also a good place to set up any application-level default
values.
There are several subdirectories under samples/ApiDemos/src/com/example/android/apis,
each corresponding to a high-level functional area of the Android
API:
App
Examples of application-level constructs such as Activities,
Alarms, Dialogs, and Services.
Content
Describes how to read assets from a file, from resources, and
from an XML file.
Graphics
Many types of graphics examples, such as arcs bitmap
manipulation, clipping, layers, and OpenGL.
Media
Examples of the MediaPlayer and the VideoView.
OS
Examples of how to invoke operating system services. As of
this writing, it shows how to use the VIBRATOR_SERVICE and SENSOR_SERVICE.
Text
Cool text tricks. The “Linkify” demo shows how to use the
autoLink attribute of the
TextView to automatically set up
links in text: the user clicks on a URL and the browser comes up, or
clicks on a phone number and the dialer appears. The “LogTextBox”
demo shows how to create a simple screen log with a LogTextBox View.
Views
All of the various Android views: buttons, text boxes,
autocompletion, date widgets, etc. You can find the dozens of
different Android GUI elements here, along with their many
options.
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.
2.1. Custom Title Demo
This technique works when the ApiDemos application stores
information about the demo in the res/strings.xml resource file:
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.
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.
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.
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.
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.
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.