private void SetupSearchBar() { mSearchView.QueryChange += async(sender, e) => { Console.WriteLine("QueryChange"); if (!string.IsNullOrEmpty(e.OldQuery) && string.IsNullOrEmpty(e.NewQuery)) { mSearchView.ClearSuggestions(); } else { // this shows the top left circular progress // you can call it where ever you want, but // it makes sense to do it when loading something in // the background. mSearchView.ShowProgress(); // simulates a query call to a data source // with a new query. var results = await DataHelper.FindSuggestionsAsync(Activity, e.NewQuery, 5, FIND_SUGGESTION_SIMULATED_DELAY); // this will swap the data and // render the collapse/expand animations as necessary mSearchView.SwapSuggestions(results); // let the users know that the background // process has completed mSearchView.HideProgress(); } }; mSearchView.SearchAction += async(sender, e) => { Console.WriteLine("SearchAction"); mLastQuery = e.CurrentQuery; var results = await DataHelper.FindColorsAsync(Activity, e.CurrentQuery); // show search results }; mSearchView.SuggestionClicked += async(sender, e) => { Console.WriteLine("SuggestionClicked"); mLastQuery = e.SearchSuggestion.GetBody(); var colorSuggestion = (ColorSuggestion)e.SearchSuggestion; var results = await DataHelper.FindColorsAsync(Activity, colorSuggestion.GetBody()); // show search results }; mSearchView.Focus += async(sender, e) => { Console.WriteLine("Focus"); // show suggestions when search bar gains focus (typically history suggestions) mSearchView.SwapSuggestions(await DataHelper.GetHistoryAsync(Activity, 3)); }; mSearchView.FocusCleared += (sender, e) => { Console.WriteLine("FocusCleared"); // set the title of the bar so that when focus is returned a new query begins mSearchView.SetSearchBarTitle(mLastQuery); // you can also set setSearchText(...) to make keep the query there when not focused and when focus returns // mSearchView.setSearchText(searchSuggestion.getBody()); }; // handle menu clicks the same way as you would in a regular activity mSearchView.MenuItemClick += (sender, e) => { Console.WriteLine("MenuItemClick"); if (e.MenuItem.ItemId == Resource.Id.action_change_colors) { mIsDarkSearchTheme = true; // demonstrate setting colors for items mSearchView.SetBackgroundColor(Color.ParseColor("#787878")); mSearchView.SetViewTextColor(Color.ParseColor("#e9e9e9")); mSearchView.SetHintTextColor(Color.ParseColor("#e9e9e9")); mSearchView.SetActionMenuOverflowColor(Color.ParseColor("#e9e9e9")); mSearchView.SetMenuItemIconColor(Color.ParseColor("#e9e9e9")); mSearchView.SetLeftActionIconColor(Color.ParseColor("#e9e9e9")); mSearchView.SetClearBtnColor(Color.ParseColor("#e9e9e9")); mSearchView.SetDividerColor(Color.ParseColor("#BEBEBE")); mSearchView.SetLeftActionIconColor(Color.ParseColor("#e9e9e9")); } else { // just print action Toast.MakeText(Activity.ApplicationContext, e.MenuItem.TitleFormatted.ToString(), ToastLength.Short).Show(); } }; // use this listener to listen to menu clicks when app:floatingSearch_leftAction="showHome" mSearchView.HomeActionClick += (sender, e) => { Console.WriteLine("HomeActionClick"); }; // Here you have access to the left icon and the text of a given suggestion // item after as it is bound to the suggestion list. You can utilize this // callback to change some properties of the left icon and the text. For example, you // can load the left icon images using your favorite image loading library, or change text color. // // Important: // Keep in mind that the suggestion list is a RecyclerView, so views are reused for different // items in the list. mSearchView.BindSuggestion += (sender, e) => { var colorSuggestion = (ColorSuggestion)e.Item; var textColor = mIsDarkSearchTheme ? "#ffffff" : "#000000"; var textLight = mIsDarkSearchTheme ? "#bfbfbf" : "#787878"; if (colorSuggestion.IsHistory) { e.LeftIcon.SetImageDrawable(ResourcesCompat.GetDrawable(Resources, Resource.Drawable.ic_history_black_24dp, null)); Util.SetIconColor(e.LeftIcon, Color.ParseColor(textColor)); e.LeftIcon.Alpha = 0.36f; } else { e.LeftIcon.Alpha = 0.0f; e.LeftIcon.SetImageDrawable(null); } e.TextView.SetTextColor(Color.ParseColor(textColor)); var text = colorSuggestion.GetBody(); if (string.IsNullOrEmpty(mSearchView.Query)) { e.TextView.Text = text; } else { text = text.Replace(mSearchView.Query, $"<font color=\"{textLight}\">{mSearchView.Query}</font>"); e.TextView.TextFormatted = Html.FromHtml(text); } }; // When the user types some text into the search field, a clear button (and 'x' to the // right) of the search text is shown. // // This listener provides a callback for when this button is clicked. mSearchView.ClearSearchAction += (sender, e) => { Console.WriteLine("ClearSearchAction"); }; }
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.activity_main); parentView = FindViewById <ViewGroup> (Resource.Id.parent_view); searchView = FindViewById <FloatingSearchView> (Resource.Id.floating_search_view); colorNameText = FindViewById <TextView> (Resource.Id.color_name_text); colorValueText = FindViewById <TextView> (Resource.Id.color_value_text); drawerLayout = FindViewById <DrawerLayout> (Resource.Id.drawer_layout); // sets the background color RefreshBackgroundColor("Xamarin Blue", "#3498DB"); searchView.QueryChange += async(sender, e) => { if (!string.IsNullOrEmpty(e.OldQuery) && string.IsNullOrEmpty(e.NewQuery)) { searchView.ClearSuggestions(); } else { // this shows the top left circular progress // you can call it where ever you want, but // it makes sense to do it when loading something in // the background. searchView.ShowProgress(); // simulates a query call to a data source // with a new query. var results = await DataHelper.FindAsync(this, e.NewQuery); // this will swap the data and // render the collapse/expand animations as necessary searchView.SwapSuggestions(results); // let the users know that the background // process has completed searchView.HideProgress(); } Console.WriteLine("QueryChange"); }; searchView.SuggestionClicked += (sender, e) => { var colorSuggestion = (ColorSuggestion)e.SearchSuggestion; RefreshBackgroundColor(colorSuggestion.Color.Name, colorSuggestion.Color.Hex); Console.WriteLine("SuggestionClicked"); }; searchView.SearchAction += (sender, e) => { Console.WriteLine("SearchAction"); }; searchView.Focus += async(sender, e) => { searchView.SwapSuggestions(await DataHelper.GetHistoryAsync(this, 3)); Console.WriteLine("Focus"); }; searchView.FocusCleared += (sender, e) => { Console.WriteLine("FocusCleared"); }; searchView.MenuItemClick += (sender, e) => { // handle menu clicks the same way as you would // in a regular activity switch (e.MenuItem.ItemId) { case Resource.Id.action_show_menu: searchView.SetLeftShowMenu(true); break; case Resource.Id.action_hide_menu: searchView.SetLeftShowMenu(false); break; } }; searchView.MenuOpened += (sender, e) => { drawerLayout.OpenDrawer(GravityCompat.Start); }; searchView.MenuClosed += (sender, e) => { drawerLayout.CloseDrawer(GravityCompat.Start); }; drawerLayout.DrawerOpened += (sender, e) => { // since the drawer might have opened as a results of // a click on the left menu, we need to make sure // to close it right after the drawer opens, so that // it is closed when the drawer is closed. searchView.CloseMenu(false); }; }