<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/searchLabel"
android:hint="@string/searchHint" />
That XML resource provides two bits of information today:
• What name should appear in the search domain button to the right of the search field, identifying to the user where they are searching (android:label
)
• What hint text should appear in the search field, to give the user a clue as to what they should be typing in (android:hint
)
Searching for Meaning in Randomness
Given all that, search is now available — Android knows your application is searchable and what search domain to use when searching from the main activity, and the activity knows how to do the search.
The options menu for this application has both local and global search options. In the case of local search, we just call onSearchRequested()
; in the case of global search, we call startSearch()
with true in the last parameter, indicating the scope is global.
Typing in a letter or two then clicking Search, will bring up the search activity and the subset of words containing what you typed, with your search query in the activity title bar. You can get the same effect if you just start typing in the main activity, since it is set up for triggering a local search.
CHAPTER 37
Development Tools
The Android SDK is more than a library of Java classes and API calls. It also includes a number of tools to assist in application development.
Much of the focus has been on the Eclipse plug-in, to integrate Android development with that IDE. Secondary emphasis has been placed on the plug-in’s equivalents for use in other IDEs or without an IDE, such as adb for communicating with a running emulator.
This chapter will cover other tools beyond those two groups.
Hierarchical Management
Android comes with a Hierarchy Viewer tool, designed to help you visualize your layouts as they are seen in a running activity in a running emulator. For example, you can determine how much space a certain widget is taking up, or try to find where a widget is hiding that does not appear on the screen.
To use the Hierarchy Viewer, you first need to fire up your emulator, install your application, launch your activity, and navigate to the spot you wish to examine. As you can see from Figure 37-1, for illustration purposes, we’ll use the ReadWrite demo application we introduced back in Chapter 18.
Figure 37-1. ReadWrite demo application
You can launch the Hierarchy Viewer via the hierarchyviewer
program, found in the tools/
directory in your Android SDK installation. This brings up the main Hierarchy Viewer window shown in Figure 37-2.
Figure 37-2. Hierarchy Viewer main window
The list on the left shows the various emulators you have opened. The number after the hyphen should line up with the number in parentheses in your emulator’s title bar.
Clicking on an emulator shows, on the right, the list of windows available for examination as you can see in Figure 37-3.
Figure 37-3. Hierarchy Viewer list of available windows
Note how there are many other windows besides our open activity, including the Launcher (i.e., the home screen), the Keyguard (i.e., the “Press Menu to Unlock” black screen you get when first opening the emulator), and so on. Your activity will be identified by application package and class (e.g., com.commonsware.android.files/...
).
Where things get interesting, though, is when you choose a window and click Load View Hierarchy. After a few seconds, the details spring into view, in a perspective called the Layout View (see Figure 37-4).
Figure 37-4. Hierarchy Viewer Layout View
The main area of the Layout View shows a tree of the various Views that make up your activity, starting from the overall system window and driving down into the individual UI widgets that users are supposed to interact with. You will see, on the lower-right branch of the tree, the LinearLayout
, Button
, and EditText
shown in the previous code listing. The remaining Views
are all supplied by the system, including the title bar.
Clicking on one of the views adds more information to this perspective and can be seen in Figure 37-5.
Figure 37-5. Hierarchy Viewer View properties
Now, in the upper-right region of the viewer, we see properties of the selected widget — in this case, the
Button. Alas, these properties do not appear to be editable.
Also, the widget is highlighted in red in the wireframe of the activity, shown beneath the properties (by default, views are shown as white outlines on a black background). This can help you ensure you have selected the right widget, if, say, you have several buttons and cannot readily tell from the tree what is what.
If you double-click on a View
in the tree, you are given a pop-up pane showing just that View
(and its children), isolated from the rest of your activity.
Down in the lower-left corner, you will see two toggle buttons, with the tree button initially selected. Clicking on the grid button puts the viewer in a whole new perspective, called the Pixel Perfect View (see Figure 37-6).
Figure 37-6. Hierarchy Viewer Pixel Perfect View
On the left, you see a tree representing the widgets and other Views
in your activity. In the middle, you see your activity (the Normal View), and on the right, you see a zoomed edition of your activity (the Loupe View).
What may not be initially obvious is that this imagery is live. Your activity is polled every so often, controlled by the Refresh Rate slider. Anything you do in the activity will then be reflected in the Pixel Perfect View’s Normal and Loupe Views.
The hairlines (cyan) overlaying the activity show the position being zoomed upon — just click on a new area to change where the Loupe View is inspecting. Of course, there is another slider to adjust how much the Loupe View is zoomed.
Delightful Dalvik Debugging Detailed, Demoed
Another tool in the Android developer’s arsenal is the Dalvik Debug Monitor Service (DDMS). This is a “Swiss Army knife,” allowing you to do everything from browse log files, update the GPS location provided by emulator, simulate incoming calls and messages, and browse the onemulator storage to push and pull files.
DDMS has a wide range of uses, so this section will not try to cover them all, rather it will cover the most useful at the time of writing.
To launch DDMS, run the ddms
program inside the tools/
directory in your Android SDK distribution. It will initially display just a tree of emulators and running programs on the left (see Figure 37-7).