Saturday, July 7, 2012

Menus

            Menus are an important part of any application. They provide familiar interfaces that reveal application functions and settings. Android offers an easy programming interface for developers to provide standardized application menus for various situations.   Android offers three fundamental types of application menus:
Options Menu
This is the primary set of menu items for an Activity. It is revealed by pressing the device MENU key. Within the Options Menu are two groups of menu items:
Icon Menu
This is the collection of items initially visible at the bottom of the screen at the press of the MENU key. It supports a maximum of six menu items. These are the only menu items that support icons and the only menu items that do not support checkboxes or radio buttons.
Expanded Menu
This is a vertical list of items exposed by the "More" menu item from the Icon Menu. It exists only when the Icon Menu becomes over-loaded and is comprised of the sixth Option Menu item and the rest.
Example:

public class OptMenuActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
      
        menu.add("Settings").setIcon(R.drawable.ic_launcher);
        menu.add("DialPad").setIcon(R.drawable.ic_launcher);
      
      
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
      
      
        if(item.getTitle()=="Settings")
        {
            Toast.makeText(getApplicationContext(), "settings", Toast.LENGTH_LONG).show();
        }
      
        else if(item.getTitle()=="DialPad")
        {
            Toast.makeText(getApplicationContext(), "DialPad", Toast.LENGTH_LONG).show();
        }
      
        return super.onOptionsItemSelected(item);
    }
     
}


Context Menu
This is a floating list of menu items that may appear when you perform a long-press on a View (such as a list item). 


Example:

package com.sai.menu;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class ShowContextMenu extends ListActivity {
   

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.names)));

        registerForContextMenu(getListView());
    }
   
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.context_menu, menu);
    }
   
    public boolean onContextItemSelected(MenuItem item) {
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
        String[] names = getResources().getStringArray(R.array.names);
        switch(item.getItemId()) {
        case R.id.edit:
            Toast.makeText(this, "You have chosen the " + getResources().getString(R.string.edit) +
                    " context menu option for " + names[(int)info.id],
                    Toast.LENGTH_SHORT).show();
            return true;
        case R.id.save:
            Toast.makeText(this, "You have chosen the " + getResources().getString(R.string.save) +
                    " context menu option for " + names[(int)info.id],
                    Toast.LENGTH_SHORT).show();
            return true;
        case R.id.delete:
            Toast.makeText(this, "You have chosen the " + getResources().getString(R.string.delete) +
                    " context menu option for " + names[(int)info.id],
                    Toast.LENGTH_SHORT).show();
            return true;
        case R.id.view:
            Toast.makeText(this, "You have chosen the " + getResources().getString(R.string.view) +
                    " context menu option for " + names[(int)info.id],
                    Toast.LENGTH_SHORT).show();
            return true;
        default:
            return super.onContextItemSelected(item);
        }
    }
Submenu
This is a floating list of menu items that is revealed by an item in the Options Menu or a Context Menu. A Submenu item cannot support nested Submenus. 




0 comments:

Post a Comment