Home > Android, Android how to, Menu > Working with Submenu in Android Activity

Working with Submenu in Android Activity


We moved. Please visit this link for this post.

 

 

 

Submenu is a another way to group your menu items.

I am going to show how to create, add items and handle items selection.

Creating Submenu and adding items:

@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		SubMenu fileMenu = menu.addSubMenu("File");
                fileMenu.add(FILE, NEW_MENU_ITEM, 0, "new");
...

Handle Submenu item selections

@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case NEW_MENU_ITEM:
			doSomething();
			break;
...

These simple code snippets shows how to create and use Submenu.
Also I am going to post whole example. Copy to your IDE and run it:

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

/**
 * Class which shows how to work with Submenus
 *
 * @author FaYna Soft Labs
 */
public class Main extends Activity {

	private static final int FILE = 0;
	private static final int EDIT = 1;

	private static final int NEW_MENU_ITEM = Menu.FIRST;
	private static final int SAVE_MENU_ITEM = NEW_MENU_ITEM + 1;

	private static final int UNDO_MENU_ITEM = SAVE_MENU_ITEM + 1;
	private static final int REDO_MENU_ITEM = UNDO_MENU_ITEM + 1;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		SubMenu fileMenu = menu.addSubMenu("File");
		SubMenu editMenu = menu.addSubMenu("Edit");
		fileMenu.add(FILE, NEW_MENU_ITEM, 0, "new");
		fileMenu.add(FILE, SAVE_MENU_ITEM, 1, "save");
		editMenu.add(EDIT, UNDO_MENU_ITEM, 0, "undo");
		editMenu.add(EDIT, REDO_MENU_ITEM, 1, "redo");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case NEW_MENU_ITEM:
			showMsg("New");
			break;
		case SAVE_MENU_ITEM:
			showMsg("Save");
			break;
		case UNDO_MENU_ITEM:
			showMsg("Undo");
			break;
		case REDO_MENU_ITEM:
			showMsg("Redo");
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	private void showMsg(String message) {
		Toast msg = Toast.makeText(Main.this, message, Toast.LENGTH_LONG);
		msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2,
				msg.getYOffset() / 2);
		msg.show();
	}
}

As you see Submenus are simple in use and also they are useful when need to group menu items.

Advertisements
Categories: Android, Android how to, Menu
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: