Searching And Search Bar In Xamarin.Forms

Targeted Audience

  • People with a basic knowledge of C# and Xamarin.
  • Knowledge of creating ListView in Xamarin.
Tools

Visual Studio with Xamarin installed.

In each platform, there is a specially designed Search Bar control. The search bar does nothing other than triggering a command when you press search or you enter text in it, as well as provide a few visual enhancements. The command is further used to search elements in the list and show them on the UI.

It looks something like the below image on UWP.

Serching And Search Bar In Xamarin.Forms

Now, let’s see how to achieve this. I hope you are familiar with the ListView. If not, then you should read my previous article “ListView And Creating List In Xamarin” or “Adding Pull To Refresh In Xamarin.Forms”. I have briefly discussed ListView in these articles. Here, I am going to tell only about the search bar. So, continuing from my previous article and considering we have a list, just put a tag “<SearchBar>” on the top of it.

SearchBar has different properties like placeholder and TextChanged which is basically an event. By hitting Enter on it, the event is automatically created.

XAML Code
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage   
  3. xmlns="http://xamarin.com/schemas/2014/forms"  
  4.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  5.              xmlns:local="clr-namespace:PracApp1"  
  6.              x:Class="PracApp1.MainPage"   
  7.              x:Name="ContentPage1">  
  8.     <StackLayout>  
  9. <SearchBar Placeholder="Search..." TextChanged="SearchBar_TextChanged"></SearchBar>  
  10.         <ListView x:Name="listView">  
  11.             <ListView.ItemTemplate>  
  12.                 <DataTemplate>  
  13.                     <TextCell Text="{Binding Name}" Detail="{BindingAddress}">  
  14. </TextCell>  
  15.                 </DataTemplate>  
  16.             </ListView.ItemTemplate>  
  17.         </ListView>  
  18.     </StackLayout>  
  19. </ContentPage>  

Now, in the xaml.cs file, create a GetContacts function which returns the list of contacts which is basically our class with two properties, as we did in previous article. Receive an argument in GetContacts which will be the text from the search bar. The argument is set to null (which is optional). If we receive a text in this argument, the function will return the list of names including that text.

Now, in the SearchBar_TextChanged function, all we need is to just send the argument to the function GetContacts which is e.TextChanged. Here, “e” is the element - in this case, the SearchBar; and the “TextChanged” is its property which gives the text in it.

C# Code
  1. IEnumerable<Contacts> GetContacts(string searchText = null)  
  2.         {  
  3.   
  4.             var contacts = new List<Contacts> {  
  5.                 new Contacts { Name = "Josh", Address = "London" },  
  6.                 new Contacts { Name = "Harry", Address = "New York" }  
  7.                };  
  8.   
  9.             if (string.IsNullOrEmpty(searchText))  
  10.                 return contacts;  
  11.             return contacts.Where(p => p.Name.StartsWith(searchText));  
  12.         }  
  13.   
  14.         private void ListView_Refreshing(object sender, EventArgs e)  
  15.         {  
  16.             listView.ItemsSource = GetContacts();  
  17.             listView.EndRefresh();  
  18.         }  
  19.   
  20.         public MainPage()  
  21.         {  
  22.             InitializeComponent();  
  23.             listView.ItemsSource = GetContacts();  
  24.             this.BindingContext = this;  
  25.   
  26.         }  
  27. private void SearchBar_TextChanged(object sender, TextChangedEventArgs e)  
  28.         {  
  29.             listView.ItemsSource = GetContacts(e.NewTextValue);  
  30.  }  

The result will be like this.

Serching And Search Bar In Xamarin.Forms
 
Serching And Search Bar In Xamarin.Forms

You can see as the value changes in the SearchBar, the function will be called and will return the list of corresponding contacts. So, this article gave you a brief introduction of how to set searching on a list.


Similar Articles