SoFunction
Updated on 2025-04-07

Detailed explanation of the use of menu in Android

Menu (menu) is a module that will definitely be used in Android. Each Android project will use Menu to provide users with selection and navigation and improve user experience. Let’s share with you through this article to use menu in Android. Friends who need it, let’s take a look.

There is a menu bar in many activities interfaces, that is, when you click a button in the upper right corner, a drop-down list will appear. The implementation of this function actually only requires the following two steps. Each activity can have its own unique menu. The specific format can be defined by yourself. The detailed creation steps are as follows.

① Create file_menu.xml in menu under res:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:andro >
  <item
    android:icon="@drawable/ic_launcher"
    android:title="File">
    <menu>
      <group android: >
        <item
          android:
          android:alphabeticShortcut="n"
          android:title="New"/>
        <item
          android:
          android:alphabeticShortcut="o"
          android:title="Open"/>
        <item
          android:
          android:alphabeticShortcut="s"
          android:title="Save"/>
      </group>
    </menu>
  </item>
</menu>

②Java code:

// Create Menu  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    (.file_menu, menu);
    return true;
  }
  // Menu click event  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (()) {
    case :
      (Menu_Resource.this, "Clicked newFile", 800).show();
      break;
    case :
      (Menu_Resource.this, "Clicked on openFile", 800).show();
      break;
    case :
      (Menu_Resource.this, "Clicked to saveFile", 800).show();
      break;
    }
    return true;
  }
  //onCreateOptionsMenuandonOptionsItemSelectedThe method isAcitivityIn-house

Summarize

The above is a detailed explanation of the use of menu in Android introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!