Click here to Skip to main content
12,701,854 members (36,031 online)
Click here to Skip to main content
Add your own
alternative version


5 bookmarked

Add Menu Items, and Respond to Them, in Android Activities

, 4 Jun 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Wherein the steps to add menu items, and respond to them being selected, is delineated

What's on the Menu

When you create an Activity in Droidio, not only is a corresponding Layout file created beneath [appName]\app\src\main\res\layout, but there is also an XML file for the Activity created beneath [appName]\app\src\main\res\menu.

This begins with a single default menu item for the Activity/Layout, namely "Settings":

<menu xmlns:android=""
    tools:context="" >
    <item android:id="@+id/action_settings"
        app:showAsAction="never" />

This is responded to in one of the three event handlers created for you by default in the Activity, namely onOptionsItemSelected() - the two others are onCreate() and onCreateOptionsMenu(). onOptionsItemSelected() looks like this at first:

public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == {
        return true;
    return super.onOptionsItemSelected(item);

If you want to add more menus to an item, and have them responded to, you just need to add an entry to the menu XML file and some code to the above-shown onOptionsItemSelected() event handler.

Here's how:

Add menu items to the menu XML file like so:

<item android:id="@+id/newdelivery"
    android:title="New Delivery"
    app:showAsAction="never" />
<item android:id="@+id/newdeliveryitem"
    android:title="New Delivery Item"
    app:showAsAction="never" />
<item android:id="@+id/newinventory"
    android:title="New Inventory"
    app:showAsAction="never" />

Now add code to the onOptionsItemSelected() event handler so that it looks something like this:

public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    switch (id) {
            Intent delIntent = new Intent(MainActivity.this, DeliveryActivity.class);
            return true;
            Intent delItemIntent = new Intent(MainActivity.this, DeliveryItemActivity.class);
            return true;
            Intent intent = new Intent(MainActivity.this, InventoryActivity.class);
            return true;
. . .
    return id == || super.onOptionsItemSelected(item);

That's all there is to it. So do it (if you need menu items, that is). Of course, you must actually have Activities named DeliveryActivity, DeliveryItemActivity, and InventoryActivity for the exact code above to work - just change it to correspond to your specific case (no pun intended (that's my story, anyway)).


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

B. Clay Shannon
Founder Across Time & Space
United States United States
I am in the process of morphing from a software developer into a portrayer of Mark Twain. My monologue (or one-man play, entitled "The Adventures of Mark Twain: As Told By Himself" and set in 1896) features Twain giving an overview of his life up till then. The performance includes the relating of interesting experiences and humorous anecdotes from Twain's boyhood and youth, his time as a riverboat pilot, his wild and woolly adventures in the Territory of Nevada and California, and experiences as a writer and world traveler, including recollections of meetings with many of the famous and powerful of the 19th century - royalty, business magnates, fellow authors, as well as intimate glimpses into his home life (his parents, siblings, wife, and children).

Peripatetic and picaresque, I have lived in eight states; specifically, besides my native California (where I was born and where I now again reside) in chronological order: New York, Montana, Alaska, Oklahoma, Wisconsin, Idaho, and Missouri.

I am also a writer of both fiction (for which I use a nom de plume, "Blackbird Crow Raven", as a nod to my Native American heritage - I am "½ Cowboy, ½ Indian") and nonfiction, including a two-volume social and cultural history of the U.S. which covers important events from 1620-2006:

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 4 Jun 2014
Article Copyright 2014 by B. Clay Shannon
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid