Create OptionsMenu using an XML menu

OptionsMenu on ActionBar

The options menu is the primary collection of menu items for an activity. The menu items are displayed on the ActionBar.

Step 1: In res/drawable/ directory add icons to be used in OptionsMenu. Take for example ic_add_white, ic_settings_white_24dp, and ic_info_outline_white.

Step 2: Firstly, create the menu in xml. Open res/menu/ directory and create a new file menu_main.xml. In menu_main.xml add the menu items using codes as shown below.

<menu xmlns:android="http://schemas.android.com/apk/res/android">
	<item
		android:id="@+id/add"
		android:title="add"
		android:icon="@drawable/ic_add_white"
		android:showAsAction="always"/>
	<item
		android:id="@+id/settings"
		android:title="Settings"
		android:icon="@drawable/ic_settings_white_24dp"/>
	<item
		android:id="@+id/info"
		android:title="Info"
		android:icon="@drawable/ic_info_outline_white"/>
</menu>		

This code will create a menu with three items, each with an id, title, and icon.

Step 3: Open app/java/package and open MainActivity.java(or the relevant java file). To specify the options menu for the activity, override onCreateOptionsMenu(). In this method, inflate the menu resource (defined in XML) into the Menu provided in the callback.

@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// TODO: Implement this method
		MenuInflater inflater = getMenuInflater();
        // Inflate menu to add items to action bar if it is present.
        inflater.inflate(R.menu.menu_main, menu);
		
		return super.onCreateOptionsMenu(menu);
	}

Step 4: When the user selects an item from the options menu (including action items in the app bar), the system calls activity’s onOptionsItemSelected() method. This method passes the MenuItem selected. You can identify the item by calling getItemId() which returns the unique ID for the menu item, or by calling getTitle().toString() which returns the title for the menu item (defined by the android:id / android:title attribute in the menu resource). This ID or title can be matched against known menu items to perform the appropriate action.

@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		Intent i1 = new Intent();
          // TODO: Implement this method
		switch(item.getTitle().toString()){
			case "add":
				// Move to NewMemoActivity.java when 'add' icon is clicked
				i1.setClass(getApplicationContext(), NewMemoActivity.class);
				startActivity(i1);
			break;
			case "Info":
				// Move to InfoActivity.java when 'Info' icon is clicked
				i1.setClass(getApplicationContext(), InfoActivity.class);
			    startActivity(i1);
		    break;
			case "Settings":
				// Move to SettingsActivity.java when 'Settings' icon is clicked
				i1.setClass(getApplicationContext(), SettingsActivity.class);
			    startActivity(i1);
				break;
		}
		return super.onOptionsItemSelected(item);
	}

Output:
Now run the app. You will find the items defined in menu_main.xml on the ActionBar.

Leave a Reply