Adding a menu to the toolbar in Android

This is a repost of an answer I wrote on Stack Overflow.

Image for post
Image for post

Menus are useful for putting multiple actions into, especially ones that are used somewhat less frequently and don’t need their own button. The menu is displayed on the toolbar in Android. I usually use a support toolbar but the directions below work just as well without the support library.

1. Make a menu xml

This is going to be in res/menu/main_menu.

  • Right click the res folder and choose New > Android Resource File.
  • Type main_menu for the File name.
  • Choose Menu for the Resource type.

Paste in the following content as a starter.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android=""

You can right click res and choose New image asset to create the ic_add icon.

2. Inflate the menu

In your activity add the following method.

public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(, menu);
return true;

3. Handle menu clicks

Also in your Activity, add the following method:

public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
return true;
return true;
return super.onOptionsItemSelected(item);

Further reading

Written by

A Flutter and Dart developer. Follow me on Twitter @suragch1 to get updates of new articles.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store