Step #5: Defining an Item

Next, we will add a low-priority action item, for an “about” screen.

Right click over the res/ directory in your project, and choose New > “Android Resource Directory” from the context menu. This will bring up a dialog to let you create a new resource directory:

Android Studio New Resource Directory Dialog
Android Studio New Resource Directory Dialog

Change the “Resource type” drop-down to be “menu”, then click “OK” to create the directory.

Then, right-click over your new res/menu/ directory and choose New > “Menu Resource File” from the context menu. Fill in actions.xml in the “New Menu Resource File” dialog:

Android Studio New Menu Resource File Dialog
Android Studio New Menu Resource File Dialog

Then click “OK” to create the file. It will open up into a menu editor. As with the layout editor, there are modes that you can toggle via icons in the tab itself:

Android Studio Menu Editor, with Toolbar Buttons Highlighted
Android Studio Menu Editor, with Toolbar Buttons Highlighted

As with the layout designer, the “Code” button shows you the XML of this resource, while the “Design” button gives you a graphical menu designer:

Android Studio Menu Designer
Android Studio Menu Designer

This editor looks and works a lot like the layout editor. The “Palette” contains things that can be dragged-and-dropped into the menu. The “Component Tree” shows the current contents of the menu. The preview area shows visually what this looks like, and the “Attributes” pane (not shown in the above screenshot) shows attributes of the selected item in the “Component Tree”.

In the “Palette” view, drag a “Menu Item” into the preview area over the right end of the app bar. This will appear as an item in an overflow area:

Android Studio Menu Designer, with a New Menu Item
Android Studio Menu Designer, with a New Menu Item

In the Attributes pane, fill in about for the “id”.

Next, we want to set the “showAsAction” value to never. To do this, click the little flag icon in the “showAsAction” field:

Android Studio Menu Designer, Attributes Pane, Showing showAsAction Flag
Android Studio Menu Designer, Attributes Pane, Showing “showAsAction” Flag

That will fold open a list of available choices:

Android Studio Menu Designer, Attributes Pane, Showing showAsAction Options
Android Studio Menu Designer, Attributes Pane, Showing “showAsAction” Options

Check the “never” checkbox in the list, then click the “Apply” button in the drop-down to close it and set “showAsAction” to never.

Then, click on the “O” button next to the “icon” field:

Android Studio Menu Designer, O Button Highlighted
Android Studio Menu Designer, “O” Button Highlighted

This will bring up a drawable resource selector:

Android Studio Drawable Resource Selection Dialog
Android Studio Drawable Resource Selection Dialog

Click on ic_about in the list of drawables, then click “OK” to accept that choice of icon. In truth, this is unnecessary, as our item should never show the icon. But, you never know when someday Google will decide to show icons for overflow menu items, so it is best to define one.

Then, click the “O” button next to the “title” field. As before, this brings up a string resource selector. Click on the “+” icon, followed by “String Value” in the resulting drop-down list. This will bring up a new string resource dialog. In the dialog, fill in menu_about as the resource name and “About” as the resource value:

Android Studio New String Resource Dialog
Android Studio New String Resource Dialog

Click “OK” to close each dialog, and you will see your new title appear in the menu editor:

Android Studio Menu Designer, Showing About Item
Android Studio Menu Designer, Showing About Item

Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.