SoFunction
Updated on 2025-04-10

Analysis of the usage examples of menus in Android development

This article describes the usage of menus for Android development. Share it for your reference, as follows:

Android phones use a button "menu" to display menus.

To implement the menu function, you must first create a menu through the method onCreateOptionMenu. There are two ways to create: one is to directly import the menu-like xml file, and the other is to directly add it using a method of Menu-like. After creating the menu, use the event listener onOptionItemSelected to listen for the events that can be triggered.

The following example has two activities, respectively, the above two methods to implement menu. Each menu has an event listener for Activity jump and exit.

1. The menu file res/menu/ under the resource file

<menu xmlns:andro>
 <item android:
   android:title="about" />
 <item android:
   android:title="quit" />
</menu>

package .Examples_04_13;
import ;
import ;
import ;
import ;
import ;
import ;
public class Activity01 extends Activity
{
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState)
 {
  (savedInstanceState);
  setContentView();
 }
 /*Create menu*/
 public boolean onCreateOptionsMenu(Menu menu)
 {
  //MenuInflater class is used to instantiate menu XML files into Menu objects
  MenuInflater inflater = getMenuInflater();
  //Set the menu interface to res/menu/  (, menu);
  return true;
 }
 /* Handle menu events*/
 public boolean onOptionsItemSelected(MenuItem item)
 {
  //Get the ID of the currently selected MenuItem,  int item_id = ();
  switch (item_id)
  {
   case :
    /* Create a new Intent object */
    Intent intent = new Intent();
    /* Specify the class to start intent */
    (, );
    /* Start a new activity */
    startActivity(intent);
    /* Close the current activity */
    ();
    break;
   case :
    ();
    break;
  }
  return true;
 }
}

package .Examples_04_13;
import ;
import ;
import ;
import ;
import ;
public class Activity02 extends Activity
{
 public void onCreate(Bundle savedInstanceState)
 {
  (savedInstanceState);
  /* Set display layout */
  setContentView(.main2);
 }
 /*Create menu*/
 public boolean onCreateOptionsMenu(Menu menu)
 {
  //Add content to menu  (0, 0, 0, );
  (0, 1, 1, );
  return true;
 }
 /*Training menu events*/
 public boolean onOptionsItemSelected(MenuItem item)
 {
  //Get the ID of the currently selected MenuItem,  int item_id = ();
  switch (item_id)
  {
   case 0:
   case 1:
    /* Create a new Intent object */
    Intent intent = new Intent();
    /* Specify the class to start intent */
    (, );
    /* Start a new activity */
    startActivity(intent);
    /* Close the current activity */
    ();
    break;
  }
  return true;
 }
}

This example uses two activities, don't forget to declare it in it.

PS: For file-related attribute functions, please refer to the online tools of this site:

Android Manifest function and permission description:
http://tools./table/AndroidManifest

For more information about Android related content, please check out the topic of this site:Android development introduction and advanced tutorial》、《Android debugging skills and solutions to common problems》、《Summary of the usage of basic Android components》、《Android View View Tips Summary》、《Android layout layout tips summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.