How to hide an Action Bar Menu Item

MartHughMartHugh USMember ✭✭
edited January 28 in Xamarin.Android

I am using v7.AppCompat and need to include a search bar on the "action bar" (represented by a v7 ToolBar)

This is OK, the following code works alright, but what I need to be able to do from my code is hide the search icon under certain conditions (like when a certain Tab is selected)

The problem I have is obtaining an object reference for the menu item because in the line

MenuItem searchitem = Menu.findItem (Resource.Id.action_search);

The type MenuItem and the method finditem do not compile.

What is the correct way to do this under v7.AppCompat?

Any ideas please ?

home.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
  <item android:id="@+id/action_search"
      android:title="Search"
      android:icon="@android:drawable/ic_menu_search"
      app:showAsAction="ifRoom|collapseActionView"
      app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>

 public override bool OnCreateOptionsMenu (IMenu menu)
        {
            MenuInflater.Inflate (Resource.Menu.home, menu);
            return base.OnCreateOptionsMenu (menu);
        }

        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);
            // Set view from the "main" layout resource
            SetContentView (Resource.Layout.main);
                       // Top 'action bar' toolbar to hold things like share and menu
                      Toolbar toolbar = FindViewById<Toolbar> (Resource.Id.toolbar);
                      //Toolbar will now take on default actionbar characteristics
              SetSupportActionBar (toolbar);

                    // get the ActionBar search menu, so we can hide it
                       MenuItem searchitem = Menu.findItem (Resource.Id.action_search);
                       searchitem.Visibility = ViewStates.Gone;

Answers

  • MartHughMartHugh USMember ✭✭

    I have pushed on further with this, but still cannot hide the search icon

     private IMenuItem m_mi;
    ...
    
     public override bool OnPrepareOptionsMenu(IMenu menu)
            {
        // get a reference to the search menu item
                m_mi = menu.FindItem(Resource.Id.action_search);
                return base.OnPrepareOptionsMenu(menu);
            }
    

    but then executing

     m_mi.Icon.SetVisible(false, true); 
    

    does not hide the icon.

    Any clues please ?

  • MartHughMartHugh USMember ✭✭

    The only way I have found to hide and reshow the icon is to call .Clear() on the menu, and then reinflate it.

    Not particularly elelgant. Is there a better way ?

  • Mart,

    I was needing to disable buttons in the appbar for an app requirement. In my case, I am using the MVVMCross framework with a MvxCachingFragmentCompatActivity and several MvxFragment classes. I have a MvxInteraction in my viewmodel for the fragment which is a really just an EventHandler that gets wired up in the fragment/view to call InvalidateOptionsMenu on the base Activity. When the MvxInteraction is raised in the ViewModel, InvalidateOptionsMenu is called which kicks off a call to OnCreateOptionsMenu. In OnCreateOptionsMenu is where I check a view model property IsBusy to determine if I should disable the buttons. Here is the code:

    View:
    In the OnCreateView method:

     ViewModel.DisableMenuButtons.WeakSubscribe((obj, args) =>
                {
                    Activity.InvalidateOptionsMenu();
                });
    

    In the OnCreateOptionsMenu:

     public override void OnCreateOptionsMenu(IMenu menu, MenuInflater inflater)
            {
                inflater.Inflate(Resource.Menu.my_menu, menu);
                base.OnCreateOptionsMenu(menu, inflater);
                if (ViewModel.IsBusy)
                {
                    ParentActivity.SupportActionBar.SetDisplayHomeAsUpEnabled(false);
                    menu.FindItem(Resource.Id.delete_item)?.SetEnabled(false);
                } else {
                    ParentActivity.SupportActionBar.SetDisplayHomeAsUpEnabled(true);
                }
            }
    

    ViewModel:

    private MvxInteraction _disableMenuButtons =
                new MvxInteraction();
    
            public IMvxInteraction DisableMenuButtons => _disableMenuButtons;
    

    Whenever you need to disable the buttons you would call _disableMenuButtons.Raise();

    In your case, you would wire an event form the viewmodel to the view to call InvalidateOptionsMenu. In the OnCreateOptionsMenu function you would use SetVisible(false) instead of SetEnabled(false).

Sign In or Register to comment.