Xamarin for Android search in action bar is not valid
I am having a problem where the ActionView property of the MenuItem containing the SearchView is always null. I'm targeting API level 22, I'm trying to just use the built-in SearchView class from the Android.Support.V7.Widget package, and I'm using the FragmentActivity function.
One note: in my application, my action bar works fine and I cannot set other menu items without issue.
For me, when I inflate the menu, I get the search menu item, but the ActionView property is always null. I get the same behavior when I use MenuItemCompat.GetActionView method.
Also, in the app, I don't see the search icon at all, but instead I see the action bar context menu, which has the label Search.
Does anyone know what I am doing wrong here? Below are my code snippets.
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]
}
}
source to share
I finally figured out my problem. The problem was that I tried to use SearchView from V7 support package but I had API level 22. Changing my code to the following fix 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]
}
}
source to share
menu_search.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"
app:showAsAction="always|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
MyActivity.cs
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Support.V4.View;
using Android.Support.V7.App;
using Android.Views;
using Android.Widget;
using Toolbar = Android.Support.V7.Widget.Toolbar;
using SearchView = Android.Support.V7.Widget.SearchView;
namespace Project.Sources.Features
{
[Activity(Label = "")]
public class SearchActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
SetContentView(Resource.Layout.search_activity);
var _toolbar = FindViewById<Toolbar>(Resource.Id.toolbar);
//Se setea la actionbar
_toolbar.SetNavigationIcon(Resource.Drawable.ic_arrow_back_white_24dp);
SetSupportActionBar(_toolbar);
//SupportActionBar.SetDisplayHomeAsUpEnabled(true);
//SupportActionBar.SetHomeButtonEnabled(true);
}
SearchView _searchView;
public override bool OnCreateOptionsMenu(IMenu menu)
{
MenuInflater.Inflate(Resource.Menu.menu_search, menu);
var item = menu.FindItem(Resource.Id.action_search);
var searchItem = MenuItemCompat.GetActionView(item);
_searchView = searchItem.JavaCast<SearchView>();
_searchView.SetIconifiedByDefault(true);
_searchView.QueryTextChange += (s, e) =>
{
Toast.MakeText(this, "Search: " + e.NewText, ToastLength.Short).Show();
};
_searchView.QueryTextSubmit += (s, e) =>
{
Toast.MakeText(this, "Searched for: " + e.Query, ToastLength.Short).Show();
e.Handled = true;
};
return base.OnCreateOptionsMenu(menu);
}
}
}
Edit: Change the type of FragmentActivity
public class TabActivity : FragmentActivity
in AppCompatActivity
public class SearchActivity : AppCompatActivity
I am using the Android.Support.V7.Widget package and it works for me.
source to share