Search code examples
c#buttonxamarincontextual-action-bar

Contextual action bar set a button to invisible


I have implemented a custom contextual action bar with two buttons : one for deleting selected items from a listview and the other one for editing selected item . What I am trying to do is to make the editButton invisible when two or more items have been selected. I tried doing it this way but nothing happens:

public void OnItemCheckedStateChanged (ActionMode mode, int position, long id, bool check)
{
    SetSubtitle (mode);
    if (listview.CheckedItemCount > 1) {
        disableButtonFlag = true;
    } else
        disableButtonFlag = false;

    self.InvalidateOptionsMenu();
}

public bool OnCreateActionMode (ActionMode mode, IMenu menu)
{
    self.MenuInflater.Inflate (Resource.Menu.CAB_menu, menu);
    if (disableButtonFlag) {
        menu.FindItem(Resource.Id.action_edit).SetVisible(false);
    } else {
        menu.FindItem(Resource.Id.action_edit).SetVisible(true);            
    }
    mode.Title = "Select Items";
    SetSubtitle (mode);
    return true;
}

Solution

  • Finally I found my mistake! It was that instead of declaring:

      if (listview.CheckedItemCount > 1) {
            disableButtonFlag = true;
        } else
            disableButtonFlag = false;
    

    within my OnCreateActionMode method and calling Activity.InvalidateOptionsMenu() in my OnItemCheckedStateChanged()method I should have declared these rows within my OnPrepareActionMode() method and then called ActionMode.Invalidate() within the OnItemCheckedStateChanged()method.