Caution

Do not forget to import the android.content.Intent package when you are working with Intents.

Finally, use the startActivity( ) method to actually start the Test Activity:

startActivity(autocomplete);

Your completed AndroidViews.java file should look like this:

package android_programmers_guide.AndroidViews;

import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.content.Intent;

public class AndroidViews extends Activity {

/** Called when the Activity is first created. / @Override public void onCreate(Bundle icicle) {

Run this application in your Android Emulator. Android should launch the AndroidViews Activity, followed quickly by the Test Activity.

In the following section, you will use these techniques to create an application that launches multiple Activities. Each of these Activities will house one View to which you can apply different options. This will give you a great deal of practice displaying and manipulating Views as well as working with Activities.

that you created in this section. You will proceed with the creation of the AndroidViews project without the Test Activity.

In this section, you are going to build an application that will allow a user to select from a number of different Views. When the user selects a View, a new Activity will be launched containing the selected View.

super.onCreate(icicle); setContentView(R.layout.main); /**Set up our Intent /

that you created in this section. You will proceed with the creation of the AndroidViews project without the Test Activity.

The tool you are going to use to offer the selections to the user is the Android Menu. Take a look the following illustration. The Menu is displayed when the user activates the Menu Button.

As you can see, selecting the Menu Button from the Android home screen produces a Wallpaper settings option. You are going to create a similar menu for your main Activity that will hold all the options for the Views that the user will be able to select from. Right now, the code of your AndroidViews.java file should look like this:

package android_programmers_guide.AndroidViews;

import android.app.Activity; import android.os.Bundle;

public class AndroidViews extends Activity {

/** Called when the Activity is first created. */ @Override public void onCreate(Bundle icicle) { super.onCreate(icicle);

setContentView(R.layout.main);

As with everything you add to your Activities, you need to import a new package to create your menu. Import the android.view.Menu into your AndroidViews Activity:

Import android.view.Menu;

To create the Menu, you need to override the onCreateOptionsMenu( ) method of the Activity. The method onCreateOptionsMenu( ) is a Boolean method that is called when the user first selects the Menu Button. You will use this method to build your Menu and add your selection items to it. Add the following code to AndroidViews.java:

@Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu);

You will add the code to create the Menu inside the onCreateOptionsMenu( ) method. The items that you need to add to the Menu are the Views that you are going to create in this project. The following is the list of View names that you will need to add to the Menu:

• AutoComplete

• RadioGroup

In the preceding code that you created to override the onCreateOptionsMenu( ) method, you passed in a Menu variable called menu. This variable represents the actual menu item that is created on the Android interface.

To add your list of items to the Menu, you will use the menu.add( ) method. The syntax for this call is as follows:

The parameter group is used to associate the menu items. You will not be using group in this example. However, the value is very important. The parameter id is used to determine what menu item was selected. Finally, the parameter title is the text that will be displayed in the Menu.

Add the following code to the onCreateOptionsMenu( ) method:

menu.

. add

0,

0,

"AutoComplete"

menu.

. add

0,

1,

"Button");

menu.

. add

0,

2,

"CheckBox");

menu.

. add

0,

3,

"EditText");

menu.

. add

0,

4,

"RadioGroup");

menu.

. add

0,

5,

"Spinner");

Your full AndroidViews.java file should now look like this:

package android_programmers_guide.AndroidViews;

import android.app.Activity; import android.os.Bundle; import android.view.Menu;

public class AndroidViews extends Activity {

/** Called when the Activity is first created. */ @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main);

@Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu);

menu.add(0, 0, menu.add(0, 1, menu.add(0, 2, menu.add(0, 3, menu.add(0, 4, menu.add(0, 5, return true;

"AutoComplete");

"Button");

"CheckBox");

"EditText");

"RadioGroup");

"Spinner");

If you execute this code as it is written, you should see the menu shown in Figure 8-1.

Figure 8-1 Menu with six Views

This is exactly what you wanted to accomplish. However, try clicking any of the options in the Menu. You do not have anything in your Activity that handles events when the user selects a Menu item.

The method you need to override to handle the calls to the Menu items is onOptionsItemSelected( ). Again, like onCreateOptionsMenu( ), onOptionsItemSelected( ) is a Boolean method that you need to override with the specific code to be executed when a Menu item is selected. The override code should look like this:

@Override public boolean onOptionsItemSelected(Menu.Item item){ }

There is one problem with this code: onOptionsItemSelected( ) is a general method that is called when any menu item is selected. You need to give onOptionsItemSelected( ) a way to differentiate between the menu items and execute code accordingly. Therefore, use a switch/case statement to help the method select between the menu items. When you created the menu items, you specified a series of numbers from 0 to 5 as the values for your menu items. You can use a call to getId( ) in your case statement to determine which menu item was selected:

return true; case 1:

return true; case 2:

return true; case 3:

return true; case 4:

return true; case 5:

return true;

return true;

In this case statement, the action for each id is currently set to return true. This will not do anything but hold open the area where you need to add code. Your AndroidViews.java file is now ready for use to create Activities that can be launched by the new menu system. The full code of AndroidViews.java should look like this:

package android_programmers_guide.AndroidViews;

import android.app.Activity; import android.os.Bundle; import android.view.Menu;

public class AndroidViews extends Activity {

/** Called when the Activity is first created. / @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main);

@Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu);

/** Add one menu item for each View in our project */

menu.add(0, 0, "AutoComplete");

return true;

/** Override onOptionsItemSelected to execute code for each menu item */ @Override public boolean onOptionsItemSelected(Menu.Item item){ }

/** Select statement to handle calls to specific menu items */ switch (item.getId()) { case 0:

return true; case 1:

return true; case 2:

return true; case 3:

return true; case 4:

return true; case 5:

return true;

return true;

With AndroidViews.java complete, you can focus on creating your other Activities. In the following sections, you will create one Activity for each View in your project and add the code to launch that view's Activity in your case statement.

0 0

Post a comment