Search code examples
androidandroid-actionbarsearchview

Xamarin Android search view in action bar is null


I am having an issue where the ActionView property of a MenuItem that contains a SearchView is always null. I am targeting API level 22, I am trying to just use the built-in SearchView class from the Android.Support.V7.Widget package, and I am using a FragmentActivity.

One thing to note is in my application my actionbar works just fine and I'm able to set other menu items no problem.

For me when I inflate the menu I am getting the search menu item, but it's ActionView property is always null. I get the same behavior when I use the MenuItemCompat.GetActionView method.

Also, in the app I do not see the search icon at all, but instead I see the action bar context menu that has the "Search" label as an option.

Does anyone have any idea what I am doing wrong here? My code snippets are below.

menu.xml

<?xml version="1.0" encoding="UTF-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:way="http://schemas.android.com/apk/res-auto" >
    <item 
        android:id="@+id/action_search"
        android:icon="@android:drawable/ic_menu_search"
        android:title="Search"
        way:showAsAction="always|collapseActionView"
        way:actionViewClass="Android.Support.V7.Widget.SearchView" />
</menu>

MyActivity.cs

namespace AndroidApp
{
    [Activity (Label = "WAY", Theme = "@style/AppTheme")]
    public class TabActivity : FragmentActivity, ActionBar.ITabListener, ViewPager.IOnPageChangeListener
    {
        private SearchView SearchView;


        [A bunch of un-realted code]

        public override bool OnCreateOptionsMenu (IMenu menu)
        {
            MenuInflater.Inflate(this.CurrentMenuId, menu);

            IMenuItem item = menu.FindItem (Resource.Id.action_search);
            var sItem = MenuItemCompat.GetActionView (item);
            this.SearchView = sItem.JavaCast<SearchView> ();

            return true;
        }

        [More un-related code]
    }
}

Solution

  • I've finally figured out my problem. The problem was that I was trying to use the SearchView from the Support V7 package, but I had my API level set to 22. Changing my code to the following fixed the problem.

    menu.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android" >
        <item 
            android:id="@+id/action_search"
            android:icon="@drawable/actionbar_button_search"
            android:title="Search"
            android:showAsAction="always"
            android:actionViewClass="android.widget.SearchView" />
    </menu>
    

    MyActivity.cs

    namespace AndroidApp
    {
        [Activity (Label = "WAY", Theme = "@style/AppTheme")]
        public class TabActivity : FragmentActivity, ActionBar.ITabListener, ViewPager.IOnPageChangeListener
        {
            private SearchView SearchView;
    
    
            [A bunch of un-realted code]
    
            public override bool OnCreateOptionsMenu (IMenu menu)
            {
                MenuInflater.Inflate(this.CurrentMenuId, menu);
    
                IMenuItem item = menu.FindItem (Resource.Id.action_search);
                this.SearchView = item.ActionView.JavaCast<Android.Widget.SearchView> ();
    
                return true;
            }
    
            [More un-related code]
        }
    }