working with menus

satya - Tuesday, September 30, 2008 10:15:42 PM

The api

The api

satya - Tuesday, September 30, 2008 10:24:51 PM

How many menus are there?

context menu
options menu
Panel menu

satya - Tuesday, September 30, 2008 10:52:18 PM

menuitem

menuitem

satya - Tuesday, September 30, 2008 10:53:03 PM

Activity

Activity

satya - Wednesday, October 01, 2008 10:30:18 AM

Override these methods in your activity


@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
	...return true;	
}

@Override
public boolean onOptionsItemSelected(MenuItem item) 
{
	...return true;
}

satya - Wednesday, October 01, 2008 10:50:48 AM

more code examples


@Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
    	menu.add(0 //Group
    			,1 //item id
    			,0 //order
    			,"append");
    	menu.add(0,2,1,"item2");
    	menu.add(0,3,2,"clear");
    	return true;	
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
    {
    	if (item.getItemId() == 1)
    	{
        	appendText("hello");
    	}
    	else if (item.getItemId() == 2)
    	{
    		appendText("item2");
    	}
    	else if (item.getItemId() == 3)
    	{
    		emptyText();
    	}
    	return true;
    }
    
    private void appendText(String text)
    {
       	TextView tv = 
       		(TextView)this.findViewById(R.id.textViewId);
       	tv.setText(tv.getText() + text);
    }
    private void emptyText()
    {
       	TextView tv = 
       		(TextView)this.findViewById(R.id.textViewId);
       	tv.setText("");
    }

satya - Wednesday, October 01, 2008 1:54:57 PM

What can I do for a "group" of menus


removeGroup(id)
setGroupCheckable(id)
setGroupEnabled(id,boolean enabled)
setGroupVisible(id,visible)

satya - Wednesday, October 01, 2008 1:57:01 PM

what is android menu setQwertyMode

what is android menu setQwertyMode

Search for: what is android menu setQwertyMode

satya - Wednesday, October 01, 2008 3:24:58 PM

MenuInflater

MenuInflater

satya - Wednesday, October 01, 2008 4:33:22 PM

Read this on menus from the howto section

Read this on menus from the howto section

satya - Wednesday, October 01, 2008 4:43:31 PM

The above explains how to use the "alternative" menus

The above explains how to use the "alternative" menus

satya - Wednesday, October 01, 2008 4:43:59 PM

How to use SELECTED_ALTERNATIVE in creating menus?

How to use SELECTED_ALTERNATIVE in creating menus?

Search for: How to use SELECTED_ALTERNATIVE in creating menus?

satya - Thursday, October 02, 2008 10:32:03 AM

what is android menu feature id?

what is android menu feature id?

Search for: what is android menu feature id?

satya - Thursday, October 02, 2008 10:33:11 AM

How is onMenuItemSelected different from onOptionsItemSelected?

How is onMenuItemSelected different from onOptionsItemSelected?

Search for: How is onMenuItemSelected different from onOptionsItemSelected?

satya - Thursday, October 02, 2008 10:36:16 AM

Some more information on menus

Some more information on menus

satya - Thursday, October 02, 2008 11:49:25 AM

What are system menus

What are system menus

satya - Thursday, October 02, 2008 4:35:34 PM

Complete source code


public class HelloWorld extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //TextView tv = new TextView(this);
        //tv.setText("Hello, Android. Say hello");
        //setContentView(tv);
        
        setContentView(R.layout.main);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
    	//add7MenuItems(menu);
    	add5SecondaryMenuItems(menu);
    	return true;
    }
    
    private void add7MenuItems(Menu menu)
    {
    	
    	menu.add(0 //Group
    			,1 //item id
    			,0 //order
    			,"append");
    	menu.add(0,2,1,"item2");
    	menu.add(0,3,2,"clear");

      	menu.add(0,4,3,"item4");
    	menu.add(0,5,4,"item5");
    	
      	menu.add(0,6,5,"item6");
    	menu.add(0,7,6,"item7");
    }
    
    private void add5SecondaryMenuItems(Menu menu)
    {
    	//Secondary items are shown just like everything else
    	
    	menu.add(0 //Group
    			,1 //item id
    			,0 //order
    			,"append");
    	menu.add(0,2,1,"item2");
    	menu.add(0,3,2,"clear");

    	menu.add(1,8,Menu.CATEGORY_SECONDARY,"g3.sec 1");
    	menu.add(1,9,Menu.CATEGORY_SECONDARY,"g3.sec 23");
    	
    	menu.add(1,30,Menu.CATEGORY_ALTERNATIVE,"g3.ALT 1");
    	menu.add(1,30,Menu.CATEGORY_ALTERNATIVE,"g3.ALT 23");
    	
    	menu.add(2,10,21,"g2 item 1");
    	menu.add(2,11,22,"g2 item 2");
    	menu.add(2,12,23,"g2 item 3");
    }
    private void tempm()
    {
/*    	
    	//Group 2
    	menu.add(1,4,3,"g1.item1");
    	menu.add(1,5,4,"g1.item3");

    	//Group 3
    	menu.add(2,6,Menu.CATEGORY_ALTERNATIVE,"g2.alt 1");
    	menu.add(2,7,Menu.CATEGORY_ALTERNATIVE,"g2.alt 23");
    	
    	//Group 4 
    	menu.add(3,8,Menu.CATEGORY_SECONDARY,"g3.sec 1");
    	menu.add(3,9,Menu.CATEGORY_SECONDARY,"g3.sec 23");

    	//Group 5 
    	menu.add(4,10,Menu.NONE,"g4. item 1");
    	menu.add(4,11,Menu.NONE,"g4. item 2");
*/    	
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
    {
    	if (item.getItemId() == 1)
    	{
        	appendText("\nhello");
    	}
    	else if (item.getItemId() == 2)
    	{
    		appendText("\nitem2");
    	}
    	else if (item.getItemId() == 3)
    	{
    		emptyText();
    	}
    	else
    	{
    		this.appendMenuItemText(item);
    	}
    	
        return super.onOptionsItemSelected(item);

    }
    
    private void appendText(String text)
    {
       	TextView tv = 
       		(TextView)this.findViewById(R.id.textViewId);
       	tv.setText(tv.getText() + text);
    }
    private void appendMenuItemText(MenuItem menuItem)
    {
    	String title = menuItem.getTitle().toString();
       	TextView tv = 
       		(TextView)this.findViewById(R.id.textViewId);
       	tv.setText(tv.getText() + "\n" + title);
    }
    private void emptyText()
    {
       	TextView tv = 
       		(TextView)this.findViewById(R.id.textViewId);
       	tv.setText("");
    }
}

satya - Friday, October 03, 2008 12:05:12 PM

What are android menu intent specifics?

What are android menu intent specifics?

Search for: What are android menu intent specifics?

satya - Friday, October 03, 2008 12:05:26 PM

read a bit on specifics

read a bit on specifics

satya - Friday, October 03, 2008 10:28:24 PM

android Runnable MenuItem how to set

android Runnable MenuItem how to set

Search for: android Runnable MenuItem how to set

satya - Wednesday, October 29, 2008 10:53:23 PM

does a submenu item id useful?

does a submenu item id useful?

satya - Wednesday, October 29, 2008 10:53:42 PM

Add commentary to menu xml files at the end

Add commentary to menu xml files at the end

satya - Saturday, January 31, 2009 10:26:36 AM

How do android menu specifics work?

How do android menu specifics work?

Search Google for: How do android menu specifics work?

Search Android Developers Group for: How do android menu specifics work?

Search Android Beginers Group for: How do android menu specifics work?

Search Google Code for: How do android menu specifics work?

Search Android Issues Database for: How do android menu specifics work?

satya - Saturday, May 02, 2009 5:37:24 PM

No significant changes in 1.5

No significant changes in 1.5

satya - Saturday, May 02, 2009 5:37:49 PM

what about the new flags

what about the new flags

satya - Friday, October 28, 2011 1:10:26 PM

ICS changes

Starting with 3.0 itself menu items can be sent to an action bar for quick display

satya - Friday, October 28, 2011 1:14:05 PM

action bar api

action bar api

satya - Friday, October 28, 2011 1:15:33 PM

my notes on action bar

my notes on action bar

satya - Friday, October 28, 2011 1:17:13 PM

here is how the menus look in the action bar

satya - Friday, October 28, 2011 1:17:56 PM

not so important tidbit

Note: On Android 2.3 and lower, the system calls onCreateOptionsMenu() to create the Options Menu when the user opens it for the first time, but on Android 3.0 and greater, the system creates it as soon as the activity is created, in order to populate the Action Bar.

satya - Friday, October 28, 2011 1:19:25 PM

The xml solution for responding to menu items

Additionally, Android 3.0 adds the ability for you to define the on-click behavior for a menu item in the menu resource XML, using the android:onClick attribute. So you don't need to implement onOptionsItemSelected(). Using the android:onClick attribute, you can specify a method to call when the user selects the menu item. Your activity must then implement the method specified in the android:onClick attribute so that it accepts a single MenuItem parameter?when the system calls this method, it passes the menu item selected.

satya - Friday, October 28, 2011 2:29:48 PM

The dynamic menu

On Android 2.3 and lower, the system calls onPrepareOptionsMenu() each time the user opens the Options Menu.

On Android 3.0 and higher, you must call invalidateOptionsMenu() when you want to update the menu, because the menu is always open. The system will then call onPrepareOptionsMenu() so you can update the menu items.

satya - Friday, October 28, 2011 2:41:17 PM

popupmenu introduced and enhanced

popupmenu introduced and enhanced

satya - Friday, October 28, 2011 2:45:05 PM

ICS api changes

ICS api changes

satya - Friday, October 28, 2011 7:29:32 PM

android popupmenu context menu difference?

android popupmenu context menu difference?

Search for: android popupmenu context menu difference?

satya - Friday, October 28, 2011 7:33:01 PM

Menu design guidelines from Android

Menu design guidelines from Android

satya - Friday, October 28, 2011 7:36:48 PM

popupmenu

popupmenu

Search Google for: popupmenu

Search Android Developers Group for: popupmenu

Search Android Beginers Group for: popupmenu

Search Google Code for: popupmenu

Search Android Issues Database for: popupmenu

satya - Friday, October 28, 2011 7:46:11 PM

here is a source code example from the api samples

here is a source code example from the api samples

satya - Friday, October 28, 2011 7:58:24 PM

How to use popup menu on a button click


public void onPopupButtonClick(View button) 
{        
    PopupMenu popup = new PopupMenu(this, button);        
    popup.getMenuInflater().inflate(R.menu.popup, popup.getMenu());
    //Or in api 14
    popup.inflate(R.menu.popup);        
    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() 
            {            
                public boolean onMenuItemClick(MenuItem item) 
                {                
                    Toast.makeText(PopupMenu1.this, 
                        "Clicked popup menu item " 
                        + item.getTitle(),                        
                        Toast.LENGTH_SHORT).show();                
                    return true;            
                }        
            }
    );        
    popup.show();
}

satya - Tuesday, November 01, 2011 3:55:46 PM

Notice the signature of onOptionsItemSelected for the options menu


public boolean onOptionsItemSelected(MenuItem item) 
{
}

satya - Tuesday, November 01, 2011 3:57:09 PM

For a popup menu see that you have to use the callback


PopupMenu.OnMenuItemClickListener
{
  public boolean onMenuItemClick(MenuItem item) 
  {
  }
}

satya - Tuesday, November 01, 2011 4:00:05 PM

here is how a popup menu on a textview base look like

satya - Tuesday, November 01, 2011 4:49:50 PM

Here is the menu xml file that is loaded


<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- This group uses the default category. -->
    <group android:id="@+id/menuGroup_Popup">
        <item android:id="@+id/popup_menu_1"
            android:title="Menu 1" />

        <item android:id="@+id/popup_menu_2"
            android:title="Menu 2" />
    </group>
</menu>

satya - Tuesday, November 01, 2011 4:51:38 PM

Notice that this menu XML file identical to an options menu

It is just the response is handled in a different callback. It is worthwhile to see how this behaves with sub menus etc.

satya - Tuesday, November 01, 2011 5:00:11 PM

Here is the code taken from my eclipse


private void showPopupMenu()
{
    TextView tv = this.getTextView();
    PopupMenu popup = new PopupMenu(this, tv);        
    //popup.getMenuInflater().inflate(
    //        R.menu.popup_menu, popup.getMenu());
    //Or in api 14
    popup.inflate(R.menu.popup_menu);        
    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() 
            {            
                public boolean onMenuItemClick(MenuItem item) 
                {
                    appendMenuItemText(item);
                    return true;
                }        
            }
    );        
    popup.show();
}

satya - 7/26/2014 8:54:09 AM

See how to document your menu files here

See how to document your menu files here

satya - 7/27/2014 4:23:33 PM

You can do this since API 11


<menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:id="@+id/menu_test_value_animator"
            android:title="Test Value Animator" 
            android:onClick="testValueAnimator"/>
</menu>

SomeActivity {
  public void testValueAnimator(MenuItem mi)
  {
  }
}

satya - 7/27/2014 4:25:21 PM

Use this along with loading that menu code


@Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
       //call the parent to attach any system level menus
       super.onCreateOptionsMenu(menu);
          MenuInflater inflater = getMenuInflater(); //from activity
          inflater.inflate(R.menu.main_menu, menu);
       return true;
    }