working with menus

The api

context menu
options menu
Panel menu

menuitem

Activity


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

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

@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("");
    }

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

what is android menu setQwertyMode

Search for: what is android menu setQwertyMode

MenuInflater

Read this on menus from the howto section

The above explains how to use the "alternative" menus

How to use SELECTED_ALTERNATIVE in creating menus?

Search for: How to use SELECTED_ALTERNATIVE in creating menus?

what is android menu feature id?

Search for: what is android menu feature id?

How is onMenuItemSelected different from onOptionsItemSelected?

Search for: How is onMenuItemSelected different from onOptionsItemSelected?

Some more information on menus

What are system menus


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("");
    }
}

What are android menu intent specifics?

Search for: What are android menu intent specifics?

read a bit on specifics

android Runnable MenuItem how to set

Search for: android Runnable MenuItem how to set

does a submenu item id useful?

Add commentary to menu xml files at the end

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?

No significant changes in 1.5

what about the new flags

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

action bar api

my notes on action bar

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.

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.

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.

popupmenu introduced and enhanced

ICS api changes

android popupmenu context menu difference?

Search for: android popupmenu context menu difference?

Menu design guidelines from Android

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

here is a source code example from the api samples


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();
}

public boolean onOptionsItemSelected(MenuItem item) 
{
}

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


<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>

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


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();
}

See how to document your menu files here


<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)
  {
  }
}

@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;
    }