Home > Android, Android how to, Menu > Operations with Options Menu items group

Operations with Options Menu items group


We moved. Please visit this link for this post.
Sometimes need to group your Options menu items.
Group is very useful when you need to work (remove, disable or hide) with several menu items.
I am going to show example about how to remove, disable/enable and hide/show group of menu items.
These operations are very simple I will not describe how they works. You need to copy this class to your IDE and run it:
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

/**
 * Class which shows what we can do with group of menu items
 *
 * @author FaYna Soft Labs
 */
public class Main extends Activity {
	private boolean isRemoved = false;
	private boolean isEnabled = false;
	private boolean isVisible = false;

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

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

	private static final int CUT_MENU_ITEM = SAVE_MENU_ITEM + 1;
	private static final int COPY_MENU_ITEM = CUT_MENU_ITEM + 1;

	private static final int LIST_MENU_ITEM = COPY_MENU_ITEM + 1;
	private static final int ICON_MENU_ITEM = LIST_MENU_ITEM + 1;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		Button rmBtn = (Button) findViewById(R.id.rmBtn);
		rmBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				isRemoved = true;
			}
		});
		Button enableBtn = (Button) findViewById(R.id.enableBtn);
		enableBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if(isEnabled) {
					isEnabled = false;
				} else {
					isEnabled = true;
				}
			}
		});
		Button visibleBtn = (Button) findViewById(R.id.visibleBtn);
		visibleBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if(isVisible) {
					isVisible = false;
				} else {
					isVisible = true;
				}
			}
		});
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		menu.clear();

		menu.add(FILE, NEW_MENU_ITEM, 0, "New");
		menu.add(FILE, SAVE_MENU_ITEM, 1, "Save");
		if(isRemoved) {
			menu.removeGroup(FILE);
		}

		menu.add(EDIT, CUT_MENU_ITEM, 2, "Cut");
		menu.add(EDIT, COPY_MENU_ITEM, 3, "Edit");
		if(isEnabled) {
			menu.setGroupEnabled(EDIT, true);
		} else {
			menu.setGroupEnabled(EDIT, false);
		}

		menu.add(VIEW, LIST_MENU_ITEM, 4, "List");
		menu.add(VIEW, ICON_MENU_ITEM, 5, "Icons");
		if(isVisible) {
			menu.setGroupVisible(VIEW, true);
		} else {
			menu.setGroupVisible(VIEW, false);
		}
		return super.onCreateOptionsMenu(menu);
	}
}

Above example shows how to remove whole menu items group

menu.removeGroup(FILE);

How to enable and disable whole menu items group

menu.setGroupEnabled(EDIT, true/false);

How to show and hide whole menu items group

menu.setGroupVisible(VIEW, true/false);

As you see it is very easy, and it is very useful when you have group of menu items.

Personally I very often use groups of menu items and also these methods.

I hope that this example is useful for you.

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: