menu menu developed by Android

  • 2020-10-31 21:58:43
  • OfStack

There are four types of menus in the Android system: options menu(option menu), context menu (context menu), sub menu (submenu), and Popup menu (pop-up menu).

First, the options menu (OptionsMenu)

1. Method Introduction:

public booleanonCreateOptionsMenu(Menu menu) : Call OptionsMenu with this method.

public booleanonOptionsItemSelected(MenuItem item) : The action that happens when a menu item is selected.

public voidonOptionsMenuClosed(Menu menu): What happens when the menu is closed.

public booleanonPrepareOptionsMenu(Menu menu) : The onPrepareOptionsMenu method is called before the options menu is displayed. You can use this method to adjust the menu according to the situation.

public booleanonMenuOpened(int featureId, Menu menu) : The action that occurs after a singles break.

2. Default styles

The default styles is popup a menu at the bottom of the screen, the menu options menu OptionsMenu we call him, 1 cases, options menu display at most 2 rows per three menu items, the menu item text icon, also known as Icon Menus, if more than 6 items, starting from the item 6 will be hidden, can appear in item 6 1 More, click More to item 6 and later a menu item, the menu item is also called Expanded Menus. The following is the introduction.

1. Override onCreateOptionsMenu(Menu menu) method


@Override 
public boolean onCreateOptionsMenu(Menu menu) 
{ 
 // Inflate the menu; this adds items to the action bar if it is present. 
 //getMenuInflater().inflate(R.menu.main, menu); 
 menu.add(0,1,1," Set up the ").setIcon(R.drawable.setting); 
 menu.add(0,3,3," Set up the ").setIcon(R.drawable.setting); 
 menu.add(0,2,2," download ").setIcon(R.drawable.download); 
 
} 

Override the onCreateOptionsMenu(Menu menu) method and add a menu item to this method, and return true, if false, the menu will not be displayed.

Note:

Most phones have an 'MENU' key at the top, which displays the menu associated with an app once it's installed on the phone.

However, starting from Android 3.0, Android no longer requires MENU to be provided on mobile devices. Although many mobile phones now offer the MENU button, one part is no longer available. In this case, Android recommends using ActionBar instead of the menu. We'll look at Android's support for ActionBar in future posts

optionsMenu on 4.2 is placed on actionbar. showAsAction="always" alive showAsAction="ifRoom" must be set in xml file to display on actionbar. Only the menu displayed on actionBar will have ICONS. To set in the code, menu.findItem (id).setShowAsAction (ES106en.es107EN_ES108en_ES109en_ES110en)

2. Override the onOptionsItemSelected(MenuItem item) method to catch the menu item event


@Override 
public boolean onOptionsItemSelected(MenuItem item) 
{ 
 // TODO Auto-generated method stub 
 if (item.getGroupId() == 0 &&item.getItemId() == 1) 
 { 
  Intent intent = new Intent(this, SecondActivity.class); 
  startActivity(intent); 
 } 
 else 
 { 
  Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show(); 
 } 
 return super.onOptionsItemSelected(item); 
} 

Context Menu (ContextMenu)

When the user presses a control for a long time, the pop-up menu is called the context menu. In Windows, we often right-click to bring up context menus.

1. Overload the onCreateContextMenu() method of Activity and call the add method of Menu to add menu item MenuItem


@Override 
public void onCreateContextMenu(ContextMenu menu, View v, 
  ContextMenuInfo menuInfo) 
{ 
  menu.add(0, 1, 0, " Red background "); 
  menu.add(0, 2, 0, " Green background "); 
  menu.add(1, 3, 0, " A white background "); 
 // TODO Auto-generated method stub 
 super.onCreateContextMenu(menu, v, menuInfo); 
} 

2. Overload the onContextItemSelected() method to respond to the menu click event


@Override 
public boolean onContextItemSelected(MenuItem item) 
{ 
 // TODO Auto-generated method stub 
  switch(item.getItemId()) { 
   case 1: 
    myText.setBackgroundColor(Color.RED); 
   break; 
   case 2: 
    myText.setBackgroundColor(Color.GREEN); 
   break; 
   case 3: 
    myText.setBackgroundColor(Color.WHITE); 
   break; 
   } 
 return true; 
} 

3. Override the registerForContextMenu() method to register the context menu for the view


 private TextView myText; 
@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
 super.onCreate(savedInstanceState); 
 setContentView(R.layout.activity_main); 
 myText = (TextView)findViewById(R.id.mytext); 
 registerForContextMenu(myText); 
} 

Submenu (SubMenu)

Submenu is a kind of menu that groups the same function for multi-level display. For example, in the "File" menu of Windows, there are submenus such as "New", "Open", and "close".

Method to create a submenu

1. Overload the onCreateOptionsMenu() method of Activity and call the addSubMenu() method of Menu to add submenu items

2. Call add() meal of SubMenu and add submenu item


@Override 
public boolean onCreateOptionsMenu(Menu menu) 
{ 
 // Inflate the menu; this adds items to the action bar if it is present. 
 SubMenu subMenu = menu.addSubMenu(0, 4, 4, " The login / registered "); 
 subMenu.add(1, 1, 1, " The login "); 
 subMenu.add(1, 2, 2, " registered "); 
 return true; 
} 

3. Overload the onOptionsItemSelected(MenuItem item) method to catch menu item events

This method has one MenuItem parameter, and you can use its getTitle and getItemId methods to determine which menu item was clicked


public boolean onOptionsItemSelected(MenuItem item) { 
 // TODO Auto-generated method stub 
 if(item.getTitle().equals(" The login ")) 
 { 
  Log.e("action:"," Click on the   registered / The login "); 
 } 
 return super.onOptionsItemSelected(item); 
} 

SubMenu is a subinterface of Menu. After adding SubMenu, its submenu items can be added through the SubMenu.add method. Images cannot be displayed on submenu items, but images can be displayed in the header of submenus, although submenu items can have check boxes and option buttons.

Note: Submenus cannot be added to submenus;

Pop-up menu (Popup)

This type of menu needs to be tied to an View, which pops up above or below the View (depending on screen space) when clicked.

Usage:

1. Establish XML menu resource file;

popup.xml


<menu xmlns:android="http://schemas.android.com/apk/res/android" > 
 <item 
  android:id="@+id/action_edit" 
  android:orderInCategory="100" 
  android:showAsAction="never" 
  android:title="edit"/> 
  <item 
  android:id="@+id/action_share" 
  android:orderInCategory="100" 
  android:showAsAction="never" 
  android:title="popup"/> 
</menu> 

2~5 steps can be implemented in the click event binding View!

2. Establish PopupMenu object, instantiate the incoming context context and the binding View;

3. Use getMenuInflater().inflate() to squeeze the XML menu file in.

4. Use setOnMenuItemClickListener object itself to set click event;

5. show itself is used to display the pop-up menu.


public void showPopuMenu(View v) 
{ 
 PopupMenu popup = new PopupMenu(MainActivity.this, v); 
 MenuInflater inflater = popup.getMenuInflater(); 
 inflater.inflate(R.menu.popup, popup.getMenu()); 
 popup.show(); 
 
} 

The above content is the menu menu developed by Android, I hope you like it.


Related articles: