Menus are a very typical UI component for any app.  A menu resource is shown in the toolbar of the app and allows the user to do common tasks such as saving information in an activity, accessing an options menu, and so on.  Here’s how you add a menu to an activity.

Add the Menu Resource Directory

Right click on the “Res” directory and go to New->Android resource directory.

Select “Menu” from the drop-down and click okay.

Create the Menu Resource File

Right click on your new “Menu” directory and click on New->Menu resource file.

Create the Options Menu From the Activity

In the activity that will host this menu, call this method.

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

Respond to Menu Item Clicks

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    if (item.getItemId() == R.id.save)
    {
        SaveProject();
    }
    return super.onOptionsItemSelected(item);
}

There you have it.  A simple menu!

Leave a Reply

Your email address will not be published. Required fields are marked *