Menu Types in Android with Tutorial

Nourhan Gehad Abdelkareim
5 min readJul 21, 2020

In Android Development, there are 3 menu types that you can use and they are used in different contexts. So We’ll here talk about the 3 types, when to use each and how to.

The 3 types:

1- Options Menu:
You can see it in the picture, the options menu is placed in the action bar. The options menu is used to display actions which relate to the whole app. (Like: Settings, Favourites, Bookmarks, etc..)

2- Context/Contextual Menu:
It’s mostly used when the user long clicks on an item in your app. So it has actions related to the pressed on item, mostly to make changes to it like deleting. Open your WhatsApp Application and long press on any of the chats you have, saw the change? This is a Context Menu in the action mode. There’s another mode which is the floating menu. You can see how the Floating one looks like in the above picture, below the contextual menu title.

3- Popup Menu
This looks like the floating context menu but it’s a different type and there’s a slight different in the usage. Mostly used with a single click, and it usually has actions that will not change the data being pressed on. For instance, sharing it.

The How To Section:

(Options Menu)

1- Create a new Android Project with an Empty Activity.
2- Create a new menu Resource file.

You can replace these steps by using Alt+Insert on your keyboard
Write a name for your file, and choose the resource type to be Menu.
Now you can see a new package was created with the name “menu”, and your new file is listed below it. You can see the <menu> </menu> tags inside the file.

3- Add two items to your menu.

Add an “id” and a “title” attribute to each <item />

4- Move to the MainActivity.java, override onCreateOptionsMenu, change the implementation to use the menu file “menu_app_actions.xml” we created.

Just start typing “onCreateOptionsMenu” and use the autocomplete.
We here decided to inflate the options menu with the menu_app_actions file we created.

5- Handle the user selection by overriding the onOptionsItemSelected function and changing its implementation.

This is the method/function we need to act upon the user’s selection.
We here used a switch case on the selected item’s id. The ids were already set in the menu’s xml file.

6- Run Your App to test the Options Menu, you created.

(Context/Contextual Menu — Floating )

1- (Repeat the steps 2 and 3 from the Options Menu above with changing the names)

2- Add dummy items to activity_main.xml and declare them in MainActivity.java

Just 3 dummy TextViews in a vertical linear layout.

3- For tvItem1 we will create a floating context menu. So do the following:

Use the registerForContextMenu() function and pass the view to it which is the tvItem1. and override the 2 functions onCreateContextMenu and onContextItemSelected.

4- Change the implementation of the functions
The onCreateContextMenu function will be used to inflate or populate the context menu with the menu items from menu_ui_elements_actions.xml file.
The onContextItemSelected will be used to know which item did the user select and to act upon the selection. Look at the code.

5- Run and see the result. (NOTE: Long click on “Item 1” to see the menu.)

(Context/Contextual Menu — Action Mode)

Complete with the previous example. For tvItem2 we will create the action mode context menu.
1- We will make an object of ActionMode and add a LongClickListener to tvItem2.

2- Implement the ActionMode.CallBack Interface and make an object of this implementation.

As you can see, we used the onCreateActionMode function to choose the file which has the menu items we want to display. which is the same one from the floating context menu menu_ui_elements_actions.xml.
We also used the onActionItemClicked to get the item the user selected and so something when he does.
At the end we set the actionMode to null we the action mode exits using the function onDestroyActionMode.

3- Last, but not the least we change the implementation of the onLongClickListener to attach the actionModeCallback to the actionMode. Then finally run to see the result.

(Popup Menu)

Complete with the previous example. For tvItem3 we will create the popup menu.
1- (Repeat the steps 2 and 3 from the Options Menu above with changing the names)

We added only one item here.

2- Add an onClickListener to tvItem3.

3- Make the MainActivity implement PopupMenu.OnMenuItemClickListener, and don’t forget to implement the method onMenuItemClick.

4- Let’s handle the user selection.

5- Let’s go back to the onClickListener of tvItem3.

6- Run to see the result.

THE END!! You can now use all the 3 types of the menus.

If You have any questions leave the a comment with it.

--

--

Nourhan Gehad Abdelkareim

Basically an Android Developer. So much into development and here to share the knowledge I have.