Skip to content

Instantly share code, notes, and snippets.

@Cheesebaron
Last active March 15, 2023 19:30
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save Cheesebaron/9670295 to your computer and use it in GitHub Desktop.
Save Cheesebaron/9670295 to your computer and use it in GitHub Desktop.
Small sample showing how to use SearchView with the Support v7 ActionBar. See the tutorial here: http://youtu.be/4FvObC44bhM
// Add this to the OnCreateOptionsMenu, where item is = menu.FindItem(Resource.Id.action_search);
MenuItemCompat.SetOnActionExpandListener(item, new SearchViewExpandListener(_adapter));
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
<?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="always|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
using Android.App;
using Android.Runtime;
using Android.Support.V4.View;
using Android.Support.V7.App;
using Android.Support.V7.Widget;
using Android.Views;
using Android.OS;
using Android.Widget;
namespace SearchViewSample
{
[Activity(Label = "SearchView Sample", MainLauncher = true, Icon = "@drawable/icon",
Theme = "@style/Theme.AppCompat.Light")]
public class SearchViewActivity : ActionBarActivity
{
private SearchView _searchView;
private ListView _listView;
private ArrayAdapter _adapter;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
var products = new[]
{
"Dell Inspiron", "HTC One X", "HTC Wildfire S", "HTC Sense", "HTC Sensation XE",
"iPhone 4S", "Samsung Galaxy Note 800",
"Samsung Galaxy S3", "MacBook Air", "Mac Mini", "MacBook Pro"
};
_listView = FindViewById<ListView>(Resource.Id.listView);
_adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, products);
_listView.Adapter = _adapter;
}
public override bool OnCreateOptionsMenu(IMenu menu)
{
MenuInflater.Inflate(Resource.Menu.main, menu);
var item = menu.FindItem(Resource.Id.action_search);
var searchItem = MenuItemCompat.GetActionView(item);
_searchView = searchItem.JavaCast<SearchView>();
_searchView.QueryTextChange += (s, e) => _adapter.Filter.InvokeFilter(e.NewText);
_searchView.QueryTextSubmit += (s, e) =>
{
//TODO: Do something fancy when search button on keyboard is pressed
Toast.MakeText(this, "Searched for: " + e.Query, ToastLength.Short).Show();
e.Handled = true;
};
return true;
}
}
}
public class SearchViewExpandListener
: Java.Lang.Object, MenuItemCompat.IOnActionExpandListener
{
private readonly IFilterable _adapter;
public SearchViewExpandListener(IFilterable adapter)
{
_adapter = adapter;
}
public bool OnMenuItemActionCollapse(IMenuItem item)
{
_adapter.Filter.InvokeFilter("");
return true;
}
public bool OnMenuItemActionExpand(IMenuItem item)
{
return true;
}
}
@mrabaev48
Copy link

how I can do it in java?(((

@nvasilescu
Copy link

_searchView = searchItem.JavaCast();

System.InvalidCastException: Unable to convert instance of type 'Android.Views.ViewGroupInvoker' to type 'android/widget/SearchView'.

The line produces the following error.

@wgrand
Copy link

wgrand commented Jul 6, 2015

I experienced the same exception with _searchView = searchItem.JavaCast();

Apparently, it has to do with AppCompat, so I changed all references to SearchView to Android.Support.V7.Widget.SearchView in SearchViewActivity.cs

@barshad
Copy link

barshad commented Sep 9, 2015

Copy link

ghost commented Dec 13, 2016

@wgrand - Exception still appear even though you change to Android.Support.V7.Widget.SearchView...

To Solve the problem, I used MenuItemCompat instead of JavaCast():

SearchView searchView = (SearchView)MenuItemCompat.GetActionView(searchItem);

@FrqSalah
Copy link

Hi everyone,
var searchItem = MenuItemCompat.GetActionView(item);
alxays returns null for me, does any one have the same problem ?

@ozgurinanoglu
Copy link

how i can do it use _adapter = new FriendAdapter(this, Android.Resource.Layout.SimpleListItem1, products);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment