65.9K
CodeProject is changing. Read more.
Home

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

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1 vote)

Jun 4, 2014

CPOL
viewsIcon

15334

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="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="hhs.app.NewDelivery" >
    <item android:id="@+id/action_settings"
        android:title="@string/action_settings"
        android:orderInCategory="100"
        app:showAsAction="never" />
</menu>

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:

    @Override
    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 == R.id.action_settings) {
            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"
        android:orderInCategory="10"
        app:showAsAction="never" />
    <item android:id="@+id/newdeliveryitem"
        android:title="New Delivery Item"
        android:orderInCategory="15"
        app:showAsAction="never" />
    <item android:id="@+id/newinventory"
        android:title="New Inventory"
        android:orderInCategory="20"
        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) {
            case R.id.newdelivery:
                Intent delIntent = new Intent(MainActivity.this, DeliveryActivity.class);
                MainActivity.this.startActivity(delIntent);
                return true;
            case R.id.newdeliveryitem:
                Intent delItemIntent = new Intent(MainActivity.this, DeliveryItemActivity.class);
                MainActivity.this.startActivity(delItemIntent);
                return true;
            case R.id.newinventory:
                Intent intent = new Intent(MainActivity.this, InventoryActivity.class);
                MainActivity.this.startActivity(intent);
                return true;
    . . .        
        }
        return id == R.id.action_settings || 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)).