Tuesday, March 13, 2012

How to add/manage menus in android application

How to add/manage menu in android application

Menues are very important part of any android applications, creation and managing those menu is little tricky in android. I will cover creation and managing menues in two steps - (You will need to create new android project and put 3 different play.png, record.png, soundon.png and stop.png into drawable folder)
  • Creating menu items
  • Managing menu items
Creating Menu Items - 





Creating the menu item is pretty simple. We need to override the "onCreateOptionsMenu(Menu menu)" method on your Activity class and need to add menues. Below is my activity class for your reference - 

package com.menuitem.test;

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

public class MenuItemTestActivity extends Activity {
private boolean isPlayStarted = false;
    /** 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(1, 101, 1, "Play").setIcon(R.drawable.play);
    menu.add(1, 102, 2, "Record").setIcon(R.drawable.record);
    menu.add(1, 103, 3, "Sound").setIcon(R.drawable.soundon);
    return super.onCreateOptionsMenu(menu);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    switch(item.getItemId()) {
    case 101:
    Toast.makeText(getApplicationContext(), "Play clicked", Toast.LENGTH_LONG).show();
    isPlayStarted = true;
    break;
    case 102:
    Toast.makeText(getApplicationContext(), "Record clicked", Toast.LENGTH_LONG).show();
    break;
    case 103:
    Toast.makeText(getApplicationContext(), "Sound clicked", Toast.LENGTH_LONG).show();
    break;

    }
    return super.onOptionsItemSelected(item);
    }
}



Managing Menu Items -

Managing menu items are nothing but enabling and disabling some of the menu options, or adding the icons depending on the selected one etc. In this, we will change the menu item's icon and title depending on some conditions. So there is "onPrepareOptionsMenu" method which is called once user click on menu button and before displaying the menues in UI, this method is called. So you can include the code to change the title, icon of the menu item in this method. Apart from that, you can also enable/disable the menu item. Please see all the methods available for MenuItem. 

At the time of first call - 



After clicking on Play menu, and again press the menu button - 



My activity class will look like this - 

package com.menuitem.test;

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

public class MenuItemTestActivity extends Activity {
private boolean isPlayStarted = false;
    /** 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(1, 101, 1, "Play").setIcon(R.drawable.play);
    menu.add(1, 102, 2, "Record").setIcon(R.drawable.record);
    menu.add(1, 103, 3, "Sound").setIcon(R.drawable.soundon);
    return super.onCreateOptionsMenu(menu);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    switch(item.getItemId()) {
    case 101:
    Toast.makeText(getApplicationContext(), "Play clicked", Toast.LENGTH_LONG).show();
    if (isPlayStarted) {
    isPlayStarted = false;
    }else {
    isPlayStarted = true;
    }
    break;
    case 102:
    Toast.makeText(getApplicationContext(), "Record clicked", Toast.LENGTH_LONG).show();
    break;
    case 103:
    Toast.makeText(getApplicationContext(), "Sound clicked", Toast.LENGTH_LONG).show();
    break;

    }
    return super.onOptionsItemSelected(item);
    }
    
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    MenuItem item = menu.findItem(101);
    if (isPlayStarted) {
    item.setTitle("Stop").setIcon(R.drawable.stop);
    } else {
    item.setTitle("Play").setIcon(R.drawable.play);
    }
    return super.onPrepareOptionsMenu(menu);
    }
}

Run the application and you will see the same screens as mentioned above.






1 comment:

  1. nice to see, everything in one place. Good work keep it up.

    ReplyDelete