Reputation: 10813
Another newbie question. I have a context menu that I apply for a ListView that simply allows the user to move items up or down, or delete the item.
I have code in onContextItemSelected() to prevent things from moving up past top or bottom of the list, etc., but I'd rather hide the context menu items in the first place if (for instance) the top item in the list is selected.
I assume that I need to do this in onCreateContextMenu, but I'm not sure how.
Here is my onCreateContextMenu code:
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mycontextmenu, menu);
}
Thanks,
wTs
Upvotes: 3
Views: 12261
Reputation: 16114
This is a solution for a PopupMenu
just in case somebody is seeking for it as I did. Here I have 3 buttons in menu
layout file for PopupMenu
and I remove some of them in some cases:
PopupMenu popup = new PopupMenu(this, this.actionButton);
popup.setOnMenuItemClickListener(this);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.menu_resume_view, popup.getMenu());
Menu menu = popup.getMenu();
if (this.resume.isPublished()) {
menu.removeItem(R.id.menu_button_publish);
}
else {
menu.removeItem(R.id.menu_button_unpublish);
menu.removeItem(R.id.menu_button_update_publish_date);
}
popup.show();
When you use PopupMenu
onPrepareOptionsPanel
is not called. So you have to get menu items by id
while creating PopupMenu
and remove those, that should not be available in a particular case.
Upvotes: 4
Reputation: 3221
You can Disable a particular item if you want to.
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo)
{
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
int position = info.position;
// use this position to decide your item clicked
menu.clear();
menu.setHeaderTitle("Context Menu Title");
String[] menuItems = getResources().getStringArray(
R.array.menu_context);
for (int i = 0; i < menuItems.length; i++)
{
menu.add(Menu.NONE, i , i, menuItems[i]);
}
menu.getItem(0).setEnabled(**Conditional check**);
menu.getItem(1).setEnabled(**Conditional check**);
menu.getItem(2).setEnabled(**Conditional check**);
}
Upvotes: 1
Reputation: 2966
In your onCreateContextMenu
method, you need to get the menu items that you potentially want to hide and set them as not visible based on the list positions.
Something like this:
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
if(info.position < 1) {
myLocationMenuItem = menu.findItem(R.id.myLocation);
myLocationMenuItem.setVisible(enable);
}
Upvotes: 14
Reputation: 7947
If a context menu is opened for a ListView
, menuInfo
will contain an object of type AdapterContextMenuInfo
, which gives you information about which item in the list was clicked. If it is the first or the last item, you can simply remove the corresponding entries from the context menu, though I'm not quite sure what happens if no entries are left.
Upvotes: 2