Creating a Test Harness for Testing Menus

Congratulations. You have learned how to create menus and how to respond to them through various callbacks. Now we'll show you a sample activity to exercise these menu APIs that you have learned so far.

The goal of this exercise is to create a simple activity with a text view in it. The text view will act like the output of a debugger. As we show and invoke menus, we will write out the invoked menu-item name and menu-item ID to this text view. The finished Menus application will look like the one shown in Figure 5-2.

Figure 5-2 shows two things of interest: the menu and the text view. The menu appears at the bottom. You will not see it, though, when you start the application; you must click the Menu button on the emulator or the device in order to see the menu. The second point of interest is the text view that lists the debug messages near the top of the screen. As you click through the available menu items, the test harness logs the menu-item names in the text view. If you click the "clear" menu item, the program clears the text view.

Sample Menus Application scratch pad to track menu items submenu:! 02 sub Item1:103 Item2

Sample Menus Application scratch pad to track menu items submenu:! 02 sub Item1:103 Item2

clear hide secondary

Figure 5-2. Sample Menus application

Note Figure 5-2 does not necessarily represent the beginning state of the sample application. We have presented it here to illustrate the menu types that we'll cover in this chapter.

Follow these steps to implement the test harness:

1. Create an XML layout file that contains the text view.

2. Create an Activity class that hosts the layout defined in step 1.

4. Add some regular menu items to the menu.

5. Add some secondary menu items to the menu.

6. Respond to the menu items.

7. Modify the AndroidManifest.xml file to show the application's proper title.

We will cover each of these steps in the following sections and provide the necessary source code to assemble the test harness.

0 0

Post a comment