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 FaYnaSoft 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);
}
}
menu.setGroupEnabled(EDIT, true
/
false
);