Last active
October 31, 2016 16:19
-
-
Save furi2/8796163 to your computer and use it in GitHub Desktop.
Handling SearchView in Xamarin.Android using OnActionExpandListener Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override public bool OnCreateOptionsMenu(IMenu menu) | |
{ | |
MenuInflater inflater = this.MenuInflater; | |
inflater.Inflate(Resource.Menu.NoteListActions, menu); | |
IMenuItem item; | |
item = menu.FindItem(Resource.Id.action_search); | |
SearchManager searchManager = (SearchManager)GetSystemService(Context.SearchService); | |
SearchView searchView = (SearchView)item.ActionView; | |
searchView.SetSearchableInfo(searchManager.GetSearchableInfo(this.ComponentName)); | |
searchView.SetIconifiedByDefault(false); | |
searchView.QueryTextSubmit += (sender, e) => | |
{ | |
string query = e.Query; | |
if (query != null && query.Trim() != "") | |
{ | |
FragmentManager fm = me.FragmentManager; | |
NoteListFragment f = fm.FindFragmentById <MyFragment> (Resource.Id.note_list_frag); | |
f.doQuery(query); | |
IMenuItem searchMenuItem = menu.FindItem(Resource.Id.action_search); | |
if (searchMenuItem != null) | |
{ | |
searchMenuItem.CollapseActionView(); | |
} | |
} | |
e.Handled = true; | |
}; | |
searchView.QueryTextChange += (sender, e) => | |
{ | |
e.Handled = true; | |
}; | |
item = menu.FindItem(Resource.Id.action_search); | |
var actionExpand = new Support.OnActionExpandListener(); | |
actionExpand.MenuItemCollaspe += (sender, e) => | |
{ | |
e.Handled = true; | |
}; | |
actionExpand.MenuItemActionExpand += (sender, e) => | |
{ | |
SearchView sv = (SearchView)e.MenuItem.ActionView; | |
sv.SetQueryHint("Search"); | |
e.Handled = true; | |
}; | |
Android.Support.V4.View.MenuItemCompat.SetOnActionExpandListener(item, actionExpand); | |
return base.OnCreateOptionsMenu(menu); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<menu xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:myapp="http://schemas.android.com/apk/res-auto" | |
> | |
<item android:id="@+id/action_search" | |
android:title="Search" | |
android:queryHint="Search" | |
android:icon="@android:drawable/ic_menu_search" | |
android:actionViewClass="android.widget.SearchView" | |
android:showAsAction="always|collapseActionView" | |
/> | |
</menu> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class OnActionExpandListener : Java.Lang.Object, Android.Support.V4.View.MenuItemCompat.IOnActionExpandListener | |
{ | |
public class MenuItemEventArg : EventArgs { | |
public IMenuItem MenuItem {get;set;} | |
public bool Handled { get; set; } | |
public MenuItemEventArg() { | |
Handled = false; | |
} | |
} | |
public delegate void MenuItemEventHandler(object sender, MenuItemEventArg e); | |
public event MenuItemEventHandler MenuItemCollaspe; | |
public event MenuItemEventHandler MenuItemActionExpand; | |
public bool OnMenuItemActionCollapse(IMenuItem item) | |
{ | |
if (MenuItemCollaspe != null) | |
{ | |
MenuItemEventArg e = new MenuItemEventArg(); | |
e.MenuItem = item; | |
MenuItemCollaspe(this, e); | |
return e.Handled; | |
} | |
return true; | |
} | |
public bool OnMenuItemActionExpand(IMenuItem item) | |
{ | |
if (MenuItemActionExpand != null) | |
{ | |
MenuItemEventArg e = new MenuItemEventArg(); | |
e.MenuItem = item; | |
MenuItemActionExpand(this, e); | |
return e.Handled; | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Needs reference to Xamarin.Android.Support.V4 component.