Android: How To Implement Option Menu In Activity or Dialog


Using Option Menu In Activity

To implement Option Menu in an Activity, we need to create a new custom Dialog class. The example code is given below, please see comments in the code for detail.

Step 1: Create a custom Menu Layout (xml file) in Menu Resources (res/menu). I named it my_menu.xml

[xml]

<?xml version="1.0" encoding="utf-8"?>
<menu
  xmlns:android="http://schemas.android.com/apk/res/android">
  
  <item 
      android:icon="@android:drawable/ic_menu_share"
      android:title="Option 1" 
      android:id="@+id/menu_option_one" 
   />

  <item 
      android:icon="@android:drawable/ic_menu_preferences" 
      android:title="Option 2" 
      android:id="@+id/menu_option_two" 
  />

  <item 
      android:icon="@android:drawable/ic_menu_info_details" 
      android:title="Option 3" 
      android:id="@+id/menu_option_three" 
  />
  
</menu>

[xml]

Step 2: Define a custom Dialog class.
package com.chohdry.adnan.androidexamples;

import android.app.Activity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class OptionMenuWithActivity extends Activity {

	/*
	 * Override function onCreateOptionsMenu(Menu menu)
	 * Get menu inflater from activity
	 * Inflate layout (assign custom menu layout)
	 * Call super class's onCreateOptionsMenu(menu)
	 */
	//
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.my_menu, menu);

		return super.onCreateOptionsMenu(menu);
	}

	/**
	 * Override function onOptionsItemSelected(MenuItem item)
	 * Identify the item
	 * Call super class's onOptionsItemSelected(MenuItem item)
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		if(item.getItemId()==R.id.menu_option_one){
			//do whatever you want
		}
		return super.onOptionsItemSelected(item);
	}
}

Using Option Menu In Dialog

Step 1 in implementation of Option Menu in a Dialog is same as above. For Step 2, class structure is different because here we need to extend our class from Dialog class rather Activity and also we need to override onMenuItemSelected(int featureId, MenuItem item) instead of onOptionsItemSelected(MenuItem item).

Step 2: Define a custom Dialog class.
package com.chohdry.adnan.androidexamples;

import android.app.Activity;
import android.app.Dialog;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

//Create a new class extended from default Dialog class
public class OptionMenuWithDialog extends Dialog {

	//Define activity type variable in class
	private Activity mActivity;

	/**
	 * Implement constructor (a custom or default passing Activity)
	 * Set class variable.
	 */
	public OptionMenuWithDialog(Activity activity) {
		super(activity, android.R.style.Theme_Translucent);
		mActivity = activity;

		//Customize your dialog here
	}

	/**
	 * Override function onCreateOptionsMenu(Menu menu)
	 * Get menu inflater from activity
	 * Inflate layout (assign custom menu layout)
	 * Call super class's onCreateOptionsMenu(menu)
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		MenuInflater inflater = mActivity.getMenuInflater();
		inflater.inflate(R.menu.my_menu, menu);

		return super.onCreateOptionsMenu(menu);
	}

	/**
	 * Override function onMenuItemSelected(int featureId, MenuItem item)
	 * Identify the item
	 * Call super class's onMenuItemSelected(int featureId, MenuItem item)
	 */
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {

		if(item.getItemId()==R.id.menu_option_one){
			//do whatever you want
		}
		return super.onOptionsItemSelected(item);
	}
}

This code is available for public use on my GitHub. Download (or get using git) sample Android Examples Project from here https://github.com/adnanyousafch/android-examples.