There are a couple of questions that still remain. You can see how to add items, submenus and generally control what the Toolbar displays but what about controlling when it is displayed or how it look.
The answer to these question is based on working with the ActionBar object and as you might expect this is not something general to all menus. The ActionBar has lots of features for showing menus in different ways and to cover it completely would take too much space and it needs a chapter all to itself.
However it is worth knowing some of the basic customizations that you can apply.
You can get the ActionBar object in an Activity using getSupportActionBar or getActionBar. Once you have the ActionBar object you can use its show() and hide() methods to remove and show it as required:
There are many other properties and methods that you can use to modify the way the action bar looks and behaves.
Things that are worth looking up are:
using a split action bar
Up Navigation
Action views
collapsible action views
action providers
navigation tabs
Summary
A menu is a hierarchy of View objects just like any UI element.
You can create a menu in code but using a menu XML resource file is the most common way of doing the job.
There are Menu objects and tags which act as containers for Item objects and tags.
Submenus can be created by nesting Menu objects within other Menu objects.
Menu items can have a title, icon, and many other attributes which govern how they are displayed.
There are four different ways you can display a menu: action bar/toolbar, context menu,contextual action mode and popup.
To make use of the action bar/toolbar and the contextual action mode in earlier versions of Android you need to use the AppCompatActivity and the other support classes
To display an action bar/toolbar all you have to do is use the onOptionsCreate event handler to create a menu, usually by inflating a menu resource.
In general you can handle click events on each menu item or you can make use of an items click event handler that the system provides that responds to a click on any of the items. It is usual to use the items click event handler.
For the action bar/Toolbar the items click event handler is defined in the Activity as onOptionsItemSelected(MenuItem item)
Where Next?
The subject of menus and how to use them is a big one and this is really just a basic introduction to the idea. From here you can learn about how to group menu items together, how to put check boxes and even completely general View objects into menus. Then there is the whole subject of using context menus and popup menus which is the subject of the next chapter.
You can download the code for this program from the CodeBin (note you have to register first).
Android Programming In Java: Starting With an App Third Edition