Menu

Menus in Android help user navigate to a specific activity based on user selection.

There are three types of menus:

  • Options menu: Triggered by pressing the hardware "Menu" button on the device. On Android 3.0 and higher, items from the options menu are presented by the action bar (App Bar)

  • Context menu: A context menu is a floating menu that appears when the user performs a long-click on an element. It provides actions that affect the selected content or context frame.

  • Popup menu (API 11 ): A popup menu displays a list of items in a vertical list that's anchored to the view that invoked the menu.

Reference: https://developer.android.com/guide/topics/ui/menus.html

Sub-menus are also supported as lists that appear when an Options or context menu is clicked.

Creating Options Menus

In this lesson you will only learn Options Menu.

The most straightforward way to create a menu is to build it in XML and then "inflate" the XML version of the menu into a Menu object. In the XML below there are three menu items defined, each with the android:title and android:titleCondensed attributes defined. Android will make the best use of space. If more space is available it uses title attribute else titleCondensed. Both are available in code when you respond to the click of an menu item.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/addition"
        android:title="Addition"
        android:titleCondensed="Add">
    </item>
    <item
        android:id="@+id/multiplication"
        android:title="Multiplication"
        android:titleCondensed="Multiply">
    </item>
    <item
        android:id="@+id/division"
        android:title="Division"
        android:titleCondensed="Div">
    </item>
</menu>

To add menu to your Activity class you override the onCreateOptionsMenus() method and use a MenuInflater to convert Xml to a menu object. Here is an example

public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

android project

The onCreateOptionsMenu() method is called when the menu is first invoked in earlier versions or when Activity is started to populate the action bar.

Override onOptionsItemSelected() method in your activity to receive Menu events. Here is an example Define main_menu.xml with the menu contents as given above, inside res/menu (menu folder may need to created if not available) folder.

Now create your MainActivity class and override the onCreateOptionsMenu and onOptionItemSelected method as below

package com.mbcc.menuexample;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem menuItem) {
        Toast.makeText(this, menuItem.getTitle(), Toast.LENGTH_SHORT).show();
        switch (menuItem.getItemId()) {
        case R.id.addition: 
           // do something
            return true;
        case R.id.multiplication: 
            // do something else
            return true;
        case R.id.division: 
            // do something else
            return true;
        default:
            return super.onOptionsItemSelected(menuItem);
        }
    }
}

Note 1: Option Menu looks different in different versions of Android. If you are deploying your app on Android 2.3 or lower, options menu panel is revealed in the bottom of the screen when you press Menu button. On Android 3.0 and higher, items on options menu are displayed on action bar in the top as action overflow items which are revealed when the user presses the action overflow icon. You can promote a few items to appear in the action bar by adding android:showAsAction="ifRoom" to the corresponding elements. From Android 3.0 Menu button is deprecated.

Note 2: Even if you're not developing for Android 3.0 or higher, you can build your own action bar layout for a similar effect using the compat libraries.

Exercise

Create options menu for your Math application so user can select 'Addition', 'Subtraction' and "Multiplication". Ensure that all options work fine when selected.

results matching ""

    No results matching ""