Android Option Menu

Android Option Menu is a primary collection of menu items that appears at the top of the activity screen. It contains a set of actions or options that can be triggered by the user, such as settings, search, and other common functionalities. Option menu is created in the activity class and appears when the user presses the Menu button on the device.

Uses of Android Option Menu

The Android Option menu provides a simple way to include common actions in an activity, rather than creating separate buttons for each action. It can be used for a variety of purposes, including:

  • Providing users with quick access to common actions
  • Customizing the behavior of an app
  • Allowing users to access additional settings and preferences

Example of Android Option Menu

Let's create a simple example of an Option menu that appears when the user presses the Menu button.

Step 1: Create a new project and open the MainActivity.java file.

public class MainActivity extends AppCompatActivity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
   }

   @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);
       return true;
   }

   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
       // Handle item selection
       switch (item.getItemId()) {
           case R.id.action_settings:
               openSettings();
               return true;
           case R.id.action_help:
               showHelp();
               return true;
           default:
               return super.onOptionsItemSelected(item);
       }
   }

   private void openSettings() {
       // Open settings activity
   }

   private void showHelp() {
       // Show help dialog
   }
}

In the code above, we have overridden the onCreateOptionsMenu() method to inflate our menu layout, which is defined in the main_menu.xml file. We have also overridden the onOptionsItemSelected() method to handle the item selection events. When the user selects an item from the menu, the corresponding method is called.

Step 2: Create a new XML file for the Option menu layout.

<menu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto">

<item
   android:id="@+id/action_settings"
   android:title="Settings"
   app:showAsAction="ifRoom"/>

<item
   android:id="@+id/action_help"
   android:title="Help"
   app:showAsAction="ifRoom"/>

</menu>

In the code above, we have defined two items in our menu layout, Settings and Help. The app:showAsAction attribute is used to specify how the items should be displayed. In this case, we have set it to ifRoom, which means that the items will be displayed as action buttons if there is enough space in the action bar.

Step 3: Run the application on an Android device or emulator. When you press the Menu button, you should see the Option menu appear, with the Settings and Help items.

In conclusion, the Android Option menu is a simple and effective way to provide users with quick access to common actions and settings within an activity. By using the Option menu, you can simplify the user interface of your app and make it more intuitive to use.