Different Types of Menu in Android

What is Menu?

Menu are the common user interface component in many types mobile application.You should use the Menu APIs to present user action and other options in your Activity.

Android menu comes in API 11.


Types of Menu

1. Option Menu
2. Context Menu
3. Popup Menu

Option Menu-
Option menu is the primary collection menu of menu item for an activity. In Android  2.3 (API level 10) or low , The option menu will be display on the bottom of screen like grid layout of 2x3 . It will show when you press the menu button of your device. In Android 3.0 (API level 11)  or higher, The option menu will display with action bar and it will expand whe we click on 3 vertical dots icon.

Option Menu default created with activity.

Steps for creating option menu 


  1. create a menu xml
  2. Register the menu in Activity
  3. code implementation for click on menu items

                                     1.  create xml for menu


In menu folder- create new xml file for menu.

menu.main.xml


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

    <item android:id="@+id/search"    
    android:title="Search" />
    
    <item android:id="@+id/menu_profile"    
    android:title="Profile" />
    
    <item android:id="@+id/setting"    
    android:title="Setting" />
    
    <item android:id="@+id/account"    
    android:title="Account" />

</menu>


                2. Register the menu in Activity


For registering option menu we have to use public boolean onCreateOptionsMenu(Menu menu) method.



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



           3.code implementation for click on menu items


 

when you want to action perform on click menu item you have to use 
public boolean onOptionsItemSelected(MenuItem item)   method.



@Overridepublic boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();

    switch(id){
        case R.id.search:
            // implement your code
            Toast.makeText(getApplicationContext(),"Search clicked",Toast.LENGTH_LONG).show();

            break;
        case R.id.profile:
            // implement your code
            Toast.makeText(getApplicationContext(),"Profile clicked",Toast.LENGTH_LONG).show();

            break;
        case R.id.setting:
            // implement your code
            Toast.makeText(getApplicationContext(),"Setting clicked",Toast.LENGTH_LONG).show();

            break;
        case R.id.account:
            // implement your code
            Toast.makeText(getApplicationContext(),"Account clicked",Toast.LENGTH_LONG).show();

            break;

        }

    return super.onOptionsItemSelected(item);
}



Context Menu
Context menu also called floating menu. It appears when user perform long click on a control. Generally we use context menu in Listview and GridView.

Steps for creating option menu

1. Register the View to which context menu should be associated by calling registerForContextMenu() and pass it the instance of view.


like registerForContextMenu(btnContext);

2. Implement the onCreateContextMenu() method in your  Activity or Fragment

when the registered view get the context event (long press event) , the system calls onCreateContextMenu()  method where you can create context menu item.Like:


@Override
public void onCreateContextMenu(ContextMenu menu, View v,
                                ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.context_menu, menu);
}

3.Implement onContextItemSelected()

Here you can code for click menu item.The getItemId() method queries the ID for the selected menu item, which you should assign to each menu item in XML using the android:id attribute.


@Overridepublic boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    switch (item.getItemId()) {
        case R.id.rename:
          // implement your code   
         Toast.makeText(getApplicationContext(),"Setting clicked",Toast.LENGTH_LONG).show();
            return true;
        case R.id.change_background:
            // implement your code   
         Toast.makeText(getApplicationContext(),"change background",Toast.LENGTH_LONG).show();
            return true;
        case R.id.setting:
            // implement your code    
        Toast.makeText(getApplicationContext(),"Setting clicked",Toast.LENGTH_LONG).show();
            return true;
        case R.id.account:
            // implement your code  
          Toast.makeText(getApplicationContext(),"Account clicked",Toast.LENGTH_LONG).show();
            return true;
        default:
            return super.onContextItemSelected(item);
    }
}

3.PopUp Menu
A popup menu displays a list of items in a vertical list that is anchor text for the view that invoke the menu.PopupMenu is available with API level 11 and higher.

                                               Steps to create Popup menu


  1. Instantiate a popup menu with its constructor, which takes the current application context and the view to which the menu should be anchored.
  2. Use MenuInflater to inflate your menu into the Menu object returned by PopMenu.getMenu(). On API leve 14 and above, you can use PopupMenu.inflate() instead.
      popupmenu.setOnClickListener(new View.OnClickListener() {
         @Override          public void onClick(View v) {
          showPopupMenu(v);
          }
        });

      3.Call PopupMenu.show()  like this




private void showPopupMenu(View v){
    PopupMenu popupMenu = new PopupMenu(MainActivity.this, v);
    popupMenu.getMenuInflater().inflate(R.menu.popup_menu, popupMenu.getMenu());

    popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

        @Override        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.delete:

                    //implement your code
                    Toast.makeText(MainActivity.this,"delete clicked",Toast.LENGTH_LONG).show();
                    break;
                case R.id.action:
                    //implement your code
                    Toast.makeText(MainActivity.this,"action clicked",Toast.LENGTH_LONG).show();
                   break;

                case R.id.save:
                    //implement your code
                    Toast.makeText(MainActivity.this,"save clicked",Toast.LENGTH_LONG).show();
                    break;
                default:
                    break;
            };
            return true;
        }
    });

    popupMenu.show();
}



output

                                                       option menu output image

                                                         context menu output image


                                                         popup menu output image

Here is the code that contain all the three menu. you can find from github





SHARE

About prabhakar jha

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment