Android Action Bar Menu
Part 1 -
Preceding tutorials in this series:
Tutorials in that series that follow:
In this set of two tutorials we will look at the basics of using the action bar to hold menu items. This provides the developer with a useful feature to save value real estate on the screen. It also provides a consistent navigational look to your apps compared to others.
The action bar was introduced with Android in API 11. This is Android Honeycomb or 3.0.x. To aid development and maintain a consistent look and feel you can download a free icon pack from the Android developer sites. The URL is below:
All the icons are contained in a single ZIP file. After unpacking the file you will see the icons for virtually all of the functions you need. There are light and dark versions for the respective action bar styles. You will see that the icons are scaled for the different screen densities: mdpi, hdpi, xhdpi and xxhdpi.
For this set of tutorials we will build a simple app where the main activity calls one of three other activities named save, settings and new. They are called from an action bar menu on the main activity which is the focus of the programming in this tutorial. The project files can be downloaded via the download button at the bottom of this page.
On setting up this project, I chose to include a blank activity with an action bar included. Using Android Studio you will be confronted with the dialog box shown below.
The key statement being that the project will create a new blank activity with action bar and that the menu resource file is included. In this example it is named “menu_main.xml”.
After the project is built, you should have a MainActivity.java file that looks similar to the one below.
It is worth noting that the project build has included the external library “appcompat_v7_21_0_3.jar”. When the project was created I stated the minimum SDK support would be API 9 or Android Gingerbread or 2.3. This library is needed so devices running this release of Android or older are able to support the action bar menus that were introduced in API 11.
If we take a closer look at the MainActvity.java file, we can see that aside from the onCreate method, two other override methods have been automatically added. These are:
These two methods are all that are needed to build and show (inflate) the action bar menu and to act on the menu items. The onCreateOptionsMenu method references the menu_main.xml file. The default file is shown below.
The purpose of the menu_main.xml file is to specify the names of the menu items, the associated icon and how it should be treated on the menu bar. The latter function is dealt with by the showAsAction attribute. The showAsAction attribute supports the following actions:
You will also notice a custom namespace xmlns:app=http://schemas.android.com/apk/res-
The onOptionsItemSelected method in the main activity is there to define the actions needed when an item is clicked. In our example a simple switch and case statement will be executed to start the activity associated with each menu item.
If we focus on our example, assuming that the java classes for the save, new and settings activities are complete, then the only changes we need for the action bar menu are confined to the onOptionsItemSelected method and the menu_main.xml file. The menu_main.xml for our example is shown below:
I have used the ifRoom|withText option for the showAsAction attribute. This will show the item if there is room on the action bar and also with Text should there be enough room. If there is a not enough room then the items are collapsed into the menu indicator comprising three vertical dots for devices API 11 and above. For older devices the physical menu button is needed to access these items.
The switch and case for calling the activities is shown in the completed MainActvity.java code sown below.
The programming is complete. Part 2 of this tutorial looks at testing the project and explores the behaviour of the showAsAction attribute.
Download Download the Project files