[Bada] Creating and Using Option and Context Menus in Bada

An option menu is created using the OptionMenu class and context menu uses the ContextMenu class. For the example in this tutorial, I’m using an application that with text on the screen and a left soft key. Clicking on the text brings up a context menu. The options menu is dispalyed when the left soft key or the option menu handle are pressed. When an item is selected in either menu, a dialog pops up to display the corresponding item’s action id.

Read more of this post

[Android] Defining a Context Menu for your View

In Building a Menu for your Android (V1.0 R1) App, a menu was created for an application. The context menu can create menus based on the current view that has focus.

In order to create context menus, the application needs to know which views will have a menu. This is done by calling registerForContextMenu with the view. In this example, a context menu will be created for two text views.

 1 package com.android.Context;
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.view.ContextMenu;
 6 import android.view.MenuItem;
 7 import android.view.View;
 8 import android.widget.TextView;
10 public class Context extends Activity {
11   /**
12    * This will keep track of which item is selected.
13    */
14   public String checkedItem = "";
16   @Override
17   public void onCreate(Bundle savedInstanceState) {
18     super.onCreate(savedInstanceState);
19     setContentView(R.layout.main);
21     registerForContextMenu(findViewById(R.id.primaryText));
22     registerForContextMenu(findViewById(R.id.secondaryText));
23   }

To set what gets displayed in the menu, the method onCreateContextMenu is overridden. This method will be told which view the context menu is being built for. Depending on the type of view that is passed in, the menuInfo parameter provides additional information. In this example, the information is not used.

25   public void onCreateContextMenu(
26       ContextMenu menu,
27       View v,
28       ContextMenu.ContextMenuInfo menuInfo)
29   {
30     // Show different menu items, depending on what is
31     // selected.
32     if (v.getId() == R.id.primaryText) 
33     {
34       menu.setHeaderTitle("Primary context");
35       addMenuItem(menu, "Primary 1", true);
36       addMenuItem(menu, "Primary 2", true);
37       addMenuItem(menu, "Primary 3", false);
38     }
39     else
40     {
41       menu.setHeaderTitle("Secondary context");
42       addMenuItem(menu, "Secondary 1", true);  
43       addMenuItem(menu, "Secondary 2", true);
44       addMenuItem(menu, "Secondary 3", false);
45     }    
46   }
48   public MenuItem addMenuItem(ContextMenu menu,
49       String itemName,
50       boolean checkable)
51   {
52     MenuItem item = menu.add(itemName);
53     item.setCheckable(checkable);
55     if (itemName.equals(checkedItem))
56     {
57       item.setChecked(true);
58     }
59     return item;
60   }

Finally, to handle item selections from the context menu, the onContextItemSelected method is overridden. In the following example, the method will simply update a text view to display what was last selected.

62   public boolean onContextItemSelected(MenuItem item)
63   {
64     TextView statusText = (TextView) findViewById(R.id.statusView);
66     if (item.isCheckable() == true)
67     {
68       // Visually display what is checked.
69       checkedItem = item.getTitle().toString();
70     }
72     statusText.setText("Last item " + item.getTitle());
73     return true;
74   }
75 }

At line 57, notice the call to set the “checked” menu item, even though the method handling the event will also be given the menu item. It may seem like it is possible simply call the setChecked method in the onContextItemSelected method, but, if you try it with this example, you’ll notice that the menu item will NEVER be checked. This is because the method onCreateContextMenu will be called every time the context menu is going to be shown (with, possibly, a different instance of the ContextMenu). In doing so, a new instance of the menu items are added to the menu via the call to the addMenuItem method.

If you compare this example with that of Building a Menu for your Android (V1.0 R1) App, you’ll notice that they follow a similar pattern for setting up menus – override the menu creation method to control what gets shown in the menu and override the receiving selection event method to control what happens when an item is selected. In fact, this same pattern is followed for an options menu (override onCreateOptionsMenu to set what gets put in there).