How to create a menu for your Android app

This tutorial will cover how to create and add menus to your Android app. You will also learn how to create submenus, dynamically add or remove items from the menu, and create a pop-up menu.

android-submenu.gif popup-menu.gif

The menu resource directory

Each menu will require a layout resource file to coordinate its contents. Layout resource files are written using XML and stored inside a dedicated directory called menu. To create the menu directory, right-click the res folder (found by navigating through Project > app) and select New > Android Resource Directory

create-settings-menu-for-android.png

Set both the directory name and resource type to menu then press OK.

menu-directory.png

Once the menu directory is in place, you can create the layout. To create a new layout resource, right-click the menu directory then select New > Menu resource file

menu-resource-file.png

Set the file name to menu_main then press OK.

menu_main.png

A layout file called menu_main.xml should then open in the editor. In the next section, we will populate the layout with items and create a menu.

The basic structure of the menu

Open the menu_main.xml layout in Code view and edit the file so it reads as follows:

<?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/menu_settings"
        android:title="@string/menu_settings"
        app:showAsAction="never" />
		
</menu>

In the above code, we define the menu resource and add an item element to it. The item will display the text which is encoded in a string resource called menu_settings. Currently, the menu_settings string resource does not exist. Let's fix that by opening up the strings resource file. You can find this file by navigating through Project > app > res > values

strings-file.png

Next, add the following string to the file:

<string name="menu_settings">Settings</string>

The menu item code also includes the line app:showAsAction="never". This line defines an attribute called showAsAction, which determines how the menu item should be displayed. It will accept the following values:

Inflating the menu

To display the menu to the user ,we must 'inflate' the menu resource file. To do this, open the MainActivity.kt file by navigating through Project > app > java > folder with your project name

mainactivity.png

Next, enter the following code below the onCreate function:

override fun onCreateOptionsMenu(menu: Menu): Boolean {
    val inflater = menuInflater
    inflater.inflate(R.menu.menu_main, menu)
    return super.onCreateOptionsMenu(menu)
}

Note you may also need to add the following statement to the top of the MainActivity.kt file to import the Menu class:

import android.view.Menu

In the above code, we override the onCreateOptionsMenu function, which is a readymade function that Android activities use to display menus. We override the method because we want to provide additional instructions for opening the menu that we made. For example, the above code uses the menuInflater class to import the contents of the menu_main.xml file into the action bar. The app is now ready to display our menu. For the remainder of this tutorial, we will explore how to format the menu and make the menu functional.

android-menu.gif

Modifying the menu based on user activity

There are times when you may wish to change the layout of the menu based on user action. For example, maybe you want a 'Download' button to appear when the user presses a button. To make a menu item appear or disappear, open the Kotlin file that manages the menu (e.g. MainActivity.kt and declare the following variables:

private var downloadItem = 1
private var showDownloadItem = false

Together these variables will help us create a menu item that is hidden by default because the boolean value is set to false.

Next, add the following line to the onCreateOptionsMenu function to incorporate the Download item into the menu:

menu.add(0, downloadItem, 0, R.string.download_item)

Note you may need to define a string called download_item in your strings.xml file:

<string name="download_item">Download</string>

Next, add a function called onPrepareOptionsMenu to your Kotlin file. The onPrepareOptionsMenu function can modify the menu's items. In this case, we set the isVisible property of the Download item to the value of the showDownloadItem variable. As it stands, the value of the showDownloadItem variable is false, so the Download item will be hidden by default.

override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
    var menuItem = menu!!.findItem(downloadItem)
    menuItem.isVisible = showDownloadItem
    menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)
    return super.onPrepareOptionsMenu(menu)
}

Note you may need to add the following statement to the top of the Kotlin file to import the MenuItem class.

import android.view.MenuItem

Next, add a function called showDownload to your Kotlin file using the code shown below. The showDownload function toggles the value of the showDownloadItem variable from false to true (or vice versa) then runs a command called invalidateOptionsMenu. The invalidateOptionsMenu command will regenerate the menu and hide/reveal the Download item because the value of the showDownloadItem variable has changed.

fun showDownload() {
    showDownloadItem = !showDownloadItem
    invalidateOptionsMenu()
}

The last thing to do is add a button to one of the app's user interface layout files such as activity_main.xml. The button should contain an onClick attribute set to showDownload, which will instruct the button to run the showDownload function and hide/reveal the Download menu item whenever the button is clicked.

show-menu-item.png

Your app should now work like this:

android-modify-menu.gif

<<< Previous

Next >>>