Create A SearchView In Xamarin Android App Using Visual Studio 2015

Introduction

Xamarin is a platform to develop cross-platform and multi-platform apps (for example, Windows phone, Android, iOS). In Xamarin platform, the code sharing concept is used. In Xamarin Studio, Visual Studio is also available.

Prerequisites 
  • Visual Studio 2015 Update 3. 
The steps given below are required to be followed in order to create a SearchView in Xamarin Android app, using Visual Studio 2015.
 
Step 1
 
Click File--> select New--> select Project. The project needs to be clicked after opening all the types of projects in Visual Studio or just click Ctrl+Shift+N.
 
 
 
Step 2
 
After opening the New Project, select Installed-->Templates-->Visual C#-->Android-->choose the Blank app (Android).
Now, give your Android app; a name (Ex- Search_View) and give the path of your project. Afterwards, click OK.
 

Step 3
 
Subsequently, go to the Solution Explorer and select Resource-->Layout. Double click to open the Main.axml page.
 
 
 
Step 4
 
After opening, the Main.axml file will open the main page designer. You can either chose Source panel for writing the code or Designer panel for designing the app, using GUI.
 
 
 
Now, delete the default "hello world" button by going to the Source panel and deleting the button code. Now, go to the MainActivity.cs page and delete C# button action code.
 
Step 5

Now, go to the Toolbox Window, scroll down and you will see all the tools and controls. You need to drag and drop the SearchView.
 
 
 
Step 6
 
Now, go to the Properties Window and edit the SearchView Id value (Ex- android:id="@+id/searchview").
 
 
 
Step 7
 
In this step, go to the Main.axml page Source Panel. Note down the SearchView Id value.
 
Main.axml
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:minWidth="25px" android:minHeight="25px">  
  2.     <SearchView android:minWidth="25px" android:minHeight="25px" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/searchview" /> </LinearLayout>  
 
 
Step 8
 
Go to the MainActivity.cs page in Solution Explorer. Write the code given below in OnCreate() Method.
 
MainActivity.cs
  1. protected override void OnCreate(Bundle bundle) {  
  2.     base.OnCreate(bundle);  
  3.     SetContentView(Resource.Layout.Main);  
  4.     SearchView search = FindViewById < SearchView > (Resource.Id.searchview);  
  5.     search.SetQueryHint("Google Search");  
  6. }  
 
Step 9
 
If you have an Android Virtual device, run the app on it, else connect your Android phone and run the app on it. Simply, connect your phone and go to Visual Studio. The connected phone will show up in the Run menu (Ex LENOVO A6020a40(Android 5.1-API 22)).
 
Click the "Run" option.
 
 

Output
 
After few seconds, the app will start running on your phone.

You will see that the SearchView is working successfully.
 
 
 
 
 
 

Summary

Hence, this was the process of creating a SearchView in Xamarin Android app, using Visual Studio 2015.