SearchBar Control In Xamarin.Forms Application For Android And UWP

SearchBar control is a View control, which provides a Search Box.

Before reading this article, please go through the article How To Create And Use XAML Content Page In Xamarin.Forms Application For Android And Universal Windows Platform.

After reading this article, you will learn how to add SearchBar control in Xamarin.Forms Application for Android and Universal Windows Platform with XAML and Visual C# in cross platform application development.

The important tools are given below, which are required to developing UWP.

  1. Windows 10 (Recommended).
  2. Visual Studio 2015 Community Edition (It is a free software available online).
  3. Using Visual studio 2015 Installer, enable Xamarin (cross platform mobile development and C#/.NET, while installing/modifying Visual Studio 2015.

Now, we can discuss step by step app development.

Step 1

Open Visual Studio 2015 -> Start -> New Project-> Select Cross-Platform (under Visual C#-> Blank app (Xamarin.Forms Portable)-> Give the suitable name for your app (XamFormSearchBar) ->OK.

 Xamarin Forms

Step 2

Now, create project “XamFormSearchBar_Droid”.

 Xamarin Forms

Choose the Target and minimum platform version for your Universal Windows Project.

 Xamarin Forms

Create project “XamFormSearchBar_UWP”.

 Xamarin Forms

Step 3

Afterwards, Visual Studio creates 6 projects and displays Getting Started.XamarinPage. Now, we have to update Xamarin.Forms Reference for Portable Project and XamFormSearchBar_Droid project.

(Please refer How To Create And Use XAML Content Page In Xamarin.Forms Application For Android And Universal Windows Platform)

Step 4

Add a XAML page for SearchBar Control Demo, right click XamFormSearchBar (Portable) project, select ADD-> NewItem.

 Xamarin Forms

Select ->CrossPlatform-> FormXamlPage-> Give the relevant name.

 Xamarin Forms

Step 5

Add SearchBar Tag, ListView with ItemTemplate and Labels in SearchBarDemo.Xaml are given below.

  1. <StackLayout Orientation="Vertical">  
  2.     <Label/>  
  3.     <Label Text=" SearchBar in Xamarin.Forms Application - UWP and Android Demo" FontSize="20" />  
  4.     <Label/>  
  5.     <Label/>  
  6.     <Label/>  
  7.     <SearchBar x:Name="searchBar" HorizontalOptions="Center" Placeholder="Search text" TextChanged="OnSearchBarTextChanged" SearchButtonPressed="OnSearchBarButtonPressed" />  
  8.     <ListView x:Name="lvSearch" IsPullToRefreshEnabled="true">  
  9.         <ListView.ItemTemplate>  
  10.             <DataTemplate>  
  11.                 <TextCell Text="{Binding SName}" /> </DataTemplate>  
  12.         </ListView.ItemTemplate>  
  13.     </ListView>  
  14. </StackLayout>  
 Xamarin Forms

Step 6

Add the code given below in SearchBarDemo.Xaml.cs.
  1. using Xamarin.Forms;  
  2. using System.Collections.ObjectModel;  
  3. public class LVsearItem {  
  4.     public string SName {  
  5.         get;  
  6.         set;  
  7.     }  
  8. }  
  9. private readonly ObservableCollection < LVsearItem > Iitem;  
  10. public SearchBarDemo() {  
  11.     InitializeComponent();  
  12.     ObservableCollection < LVsearItem > items = new ObservableCollection < LVsearItem > ();  
  13.     items.Add(new LVsearItem() {  
  14.         SName = "C Sharp corner"  
  15.     });  
  16.     items.Add(new LVsearItem() {  
  17.         SName = "C Sharp"  
  18.     });  
  19.     items.Add(new LVsearItem() {  
  20.         SName = "C Sharp Programming"  
  21.     });  
  22.     items.Add(new LVsearItem() {  
  23.         SName = "Microsoft"  
  24.     });  
  25.     items.Add(new LVsearItem() {  
  26.         SName = "Xamarin"  
  27.     });  
  28.     items.Add(new LVsearItem() {  
  29.         SName = "Xamarin Forms"  
  30.     });  
  31.     items.Add(new LVsearItem() {  
  32.         SName = "Xamarin Native Application Development"  
  33.     });  
  34.     Iitem = items;  
  35.     lvSearch.ItemsSource = Iitem;  
  36. }  
  37. private void FilterNames() {  
  38.     string filter = searchBar.Text;  
  39.     lvSearch.BeginRefresh();  
  40.     if (string.IsNullOrWhiteSpace(filter)) {  
  41.         lvSearch.ItemsSource = Iitem;  
  42.     } else {  
  43.         lvSearch.ItemsSource = Iitem.Where(x => x.SName.ToLower().Contains(filter.ToLower()));  
  44.     }  
  45.     lvSearch.EndRefresh();  
  46. }  
  47. void OnSearchBarTextChanged(object sender, TextChangedEventArgs args) {  
  48.     FilterNames();  
  49. }  
  50. void OnSearchBarButtonPressed(object sender, EventArgs args) {  
  51.     FilterNames();  
  52. }  
 Xamarin Forms

Step 7

Open (double click) the file App.cs in the Solution Explorer-> XamFormSearchBar(portable) and set the Root page.

 Xamarin Forms

Step 8

We will test Android and UWP. Thus, we can set the Multiple Startup Projects as XamFormSearchBar.Droid and XamFormSearchBar.UWP (Universal Windows).

 Xamarin Forms

Step 9

Change the Configuration Manager settings, go to Build -> Configuration Manager, uncheck all the build and deploy options to the iOS, Windows, WinPhone. Check the Droid and UWP.

 Xamarin Forms

Step 10

Deploy your app in Local Machine and the output of the XamFormSearchBar app is given below.

 Xamarin Forms

Entering Search Text in a Search Bar is given below. 

 Xamarin Forms

Search Text in Search Bar is given below. 

 Xamarin Forms

Summary

Now, you have successfully created and tested SearchBar control in Xamarin.Forms Applications for cross platform application development, using Visual C# and Xamarin.

 

Up Next
    Ebook Download
    View all
    Learn
    View all