Adding a menu to the toolbar in Android

Suragch
2 min readJan 5, 2019

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

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="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_add"
android:icon="@drawable/ic_add"
app:showAsAction="ifRoom"
android:title="Add">
</item>
<item
android:id="@+id/action_settings"
app:showAsAction="never"
android:title="Settings">
</item>
</menu>

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

2. Inflate the menu

--

--