First, create the Xamarin forms project and then update the all Nuget packages in your project.
Write this code in your MainPage.Xaml,
- <AbsoluteLayout>
- <Entry TextChanged="SearchBar_OnTextChanged" BackgroundColor="#f9f9f9" TextColor="#FF464859" FontSize="16" PlaceholderColor="#646b7a" x:Name="searchBar" Placeholder="Type here..." AbsoluteLayout.LayoutBounds="15,70,285,38" AbsoluteLayout.LayoutFlags="None"/>
- <ListView x:Name="countryListView" IsVisible="False" CachingStrategy="RecycleElement" BackgroundColor="White" ItemTapped="ListView_OnItemTapped" AbsoluteLayout.LayoutBounds="20,110,269,160" AbsoluteLayout.LayoutFlags="None">
- <ListView.ItemTemplate>
- <DataTemplate>
- <ViewCell>
- <Frame>
- <StackLayout BackgroundColor="White">
- <Label Text="{Binding .}" FontSize="16" TextColor="#FF464859"/>
- </StackLayout>
- </Frame>
- </ViewCell>
- </DataTemplate>
- </ListView.ItemTemplate>
- </ListView>
- </AbsoluteLayout>
Initial declared ObservableCollection,
- ObservableCollection<string> data = new ObservableCollection<string>();
- public async void ListOfStore() //List of Countries
- {
- try
- {
- data.Add("Afghanistan");
- data.Add("Austria");
- data.Add("Australia");
- data.Add("Azerbaijan");
- data.Add("Bahrain");
- data.Add("Bangladesh");
- data.Add("Belgium");
- data.Add("Botswana");
- data.Add("China");
- data.Add("Colombia");
- data.Add("Denmark");
- data.Add("Kmart");
- data.Add("Pakistan");
- }
- catch (Exception ex)
- {
- await DisplayAlert("", ""+ex, "Ok");
- }
- }
And now call this method ListOfStore() in Constructor,
- public MainPage()
- {
- InitializeComponent();
- ListOfStore();
- }
Now we create SearchBar_OnTextChanged method in MainPage.xaml.cs class,
- private void SearchBar_OnTextChanged(object sender, TextChangedEventArgs e)
- {
- countryListView.IsVisible = true;
- countryListView.BeginRefresh();
- try
- {
- var dataEmpty = data.Where(i => i.ToLower().Contains(e.NewTextValue.ToLower()));
- if (string.IsNullOrWhiteSpace(e.NewTextValue))
- countryListView.IsVisible = false;
- else if (dataEmpty.Max().Length == 0)
- countryListView.IsVisible = false;
- else
- countryListView.ItemsSource = data.Where(i => i.ToLower().Contains(e.NewTextValue.ToLower()));
- }
- catch (Exception ex)
- {
- countryListView.IsVisible = false;
- }
- countryListView.EndRefresh();
- }
Now we create ItemTapped method in MainPage.xaml.cs class,
- private void ListView_OnItemTapped(Object sender, ItemTappedEventArgs e)
- {
- //EmployeeListView.IsVisible = false;
- String listsd = e.Item as string;
- searchBar.Text = listsd;
- countryListView.IsVisible = false;
- ((ListView)sender).SelectedItem = null;
- }
Here is the full code,
- ObservableCollection<string> data = new ObservableCollection<string>();
- public MainPage()
- {
- InitializeComponent();
- ListOfStore();
- }
- public async void ListOfStore() //List of Countries
- {
- try
- {
- data.Add("Afghanistan");
- data.Add("Austria");
- data.Add("Australia");
- data.Add("Azerbaijan");
- data.Add("Bahrain");
- data.Add("Bangladesh");
- data.Add("Belgium");
- data.Add("Botswana");
- data.Add("China");
- data.Add("Colombia");
- data.Add("Denmark");
- data.Add("Kmart");
- data.Add("Pakistan");
- }
- catch (Exception ex)
- {
- await DisplayAlert("", ""+ex, "Ok");
- }
- }
- private void SearchBar_OnTextChanged(object sender, TextChangedEventArgs e)
- {
- countryListView.IsVisible = true;
- countryListView.BeginRefresh();
- try
- {
- var dataEmpty = data.Where(i => i.ToLower().Contains(e.NewTextValue.ToLower()));
- if (string.IsNullOrWhiteSpace(e.NewTextValue))
- countryListView.IsVisible = false;
- else if (dataEmpty.Max().Length == 0)
- countryListView.IsVisible = false;
- else
- countryListView.ItemsSource = data.Where(i => i.ToLower().Contains(e.NewTextValue.ToLower()));
- }
- catch (Exception ex)
- {
- countryListView.IsVisible = false;
- }
- countryListView.EndRefresh();
- }
- private void ListView_OnItemTapped(Object sender, ItemTappedEventArgs e)
- {
- //EmployeeListView.IsVisible = false;
- String listsd = e.Item as string;
- searchBar.Text = listsd;
- countryListView.IsVisible = false;
- ((ListView)sender).SelectedItem = null;
- }
- }
Here is a simple screenshot,


Asad NaeemPosted Mar 1, 2022, 5:39 AM
How to change the direction of this listview to upwards.
reshma masuPosted Aug 24, 2020, 5:19 AM
For large data you said to have JSON data to be serialized right ... i have that ready .. how do i link it with search bar now? can you please show me snippets of its code .. thanks in advance.... can you please reply for this?
Ahmed NabilPosted Feb 24, 2020, 5:22 AM
Thanks for your effort , i have a problem with drop down list it`s shown with out any content but if i press on any item of list it return value , so could you help me
Mark HenkePosted Sep 30, 2019, 10:24 AM
How large volume of data??? is only 13 rows... I need possible 10000 or more to search... I will limit to only search after 3 letters!
ashok yadavPosted Aug 19, 2019, 8:46 AM
There is one problem here can you help me, when we add control below listview like label or button listview occupy the space, we need to overlap the listview.
Anil JainPosted May 23, 2019, 7:58 AM
Simple and awesome!! Not a single error!!
Laxmidhar SahooPosted Apr 14, 2019, 8:08 AM
Thanks for a valuable article
Kamran KalwarPosted Apr 12, 2019, 12:53 AM
Helpful Dude????
Faiz AhmedPosted Apr 11, 2019, 9:27 AM
Wow Man! that's great blog. It's very easy way to make autocomplete testfield.