-
-
Save Cheesebaron/9670295 to your computer and use it in GitHub Desktop.
| // 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; | |
| } | |
| } |
_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.
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
Can we add button (Voice Search) in SearchView?
I want to implement something like this https://material-design.storage.googleapis.com/publish/material_v_4/material_ext_publish/0B6Okdz75tqQsUENaeGRvVks0Tms/patterns_search_expandable2.png
@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);
Hi everyone,
var searchItem = MenuItemCompat.GetActionView(item);
alxays returns null for me, does any one have the same problem ?
how i can do it use _adapter = new FriendAdapter(this, Android.Resource.Layout.SimpleListItem1, products);
how I can do it in java?(((