Create An Auto Suggest Box In Xamarin Android App Using Visual Studio 2015 Update 3

Introduction

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

Auto Suggest Box is called, if you type any word in the textbox. Type the first letter and automatically it will show the word (Ex: type "i" and it starts showing words that start with "i" such as India, Israel etc).

Prerequisites

  • Visual Studio 2015 Update 3

The steps, given below, need to be followed in order to create an Auto Suggest box in Xamarin Android app, using Visual Studio 2015 Update 3.

Step 1 - Click file--> select New--> select Project. After clicking the project, click open all the types of projects in Visual Studio
or click (Ctrl+Shift+N)



Step 2 - After opening the New Project, select Installed-->Templates-->Visual C#-->Android-->choose the Blank app (Android).

Give your Android app name (Ex:sample) and give the path of your project. Click OK.



Step 3 - Go to Solution Explorer. In Solution Explorer, have all the files and source in your project.

Select Resource-->Layout-->double click to open main.axml page. If you want to select the source, write XAML code.

If you want to design, choose the designer Window and you can design your app.



Step 4 - After opening main.axml, file will open the main page designer. In this page, you can design, as per the type you want.



You will delete the Default hello world button. Go to the source panel and you can see the button coding. You need to delete it.

After deleting XAML code, delete C# button action code. Go to the MainActivity.cs page. You need to delete the button code.

Step 5 - Go to the toolbox Window which has all the types of  tools and controls.You will go to toolbox Window and scroll down.

You will see all the tools and controls. 

You will drag and drop TextView and AutoCompleteTextView control in your main.axml page.



Step 6 - After dragging and dropping, TextView and AutoCompleteTextView will be show in your app.



Step 7 - Go to the properties Window and edit the text value(Ex: text=country).



Step 8 - Go to the Main.axml source tap and you will change the Id value or note the value.



Main.axml

  1. <TextView android:text="Country" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textView1" />  
  2. <AutoCompleteTextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/autoCompleteTextView1" />  
Step 9 - Add one XML page and it's name is called list.xml

Go to Solution Explorer--> Resource-->Layout Right click Layout --> Add--> New item.



Step 10 - Select Visual c# --> select XML file--> give the name of XML file and click add.



Step 11 - Write TextView code. TextView shows the auto text values.
The code, given below, is required to write the list.xml file-

list.xml

  1. <TextView xmlns:android="http://schemas.android.com/apk/res/android"   
  2. android:layout_width="fill_parent"   
  3. android:layout_height="fill_parent"   
  4. android:padding="10dp"   
  5. android:textSize="16sp"   
  6. android:textColor="#fff">   
  7. </TextView>  


Step 12 - Go to the MainActivity.cs page and write C# code, given below-

ManiActivity.cs
  1. public class MainActivity: Activity  
  2. {  
  3.         //declare string array CONTRIES   
  4.         static string[] COUNTRIES = new string[] {  
  5.             "Australia",  
  6.             "Austria",  
  7.             "Azerbaijan",  
  8.             "Bahrain",  
  9.             "Bangladesh",  
  10.             "Barbados",  
  11.             "Belarus",  
  12.             "Belgium",  
  13.             "Belize",  
  14.             "Benin",  
  15.             "Bermuda",  
  16.             "Bhutan",  
  17.             "Bolivia",  
  18.             "Bulgaria",  
  19.             "Burkina Faso",  
  20.             "Burundi",  
  21.             "Iceland",  
  22.             "India",  
  23.             "Indonesia",  
  24.             "Iran",  
  25.             "Iraq",  
  26.             "Ireland",  
  27.             "Israel",  
  28.             "Italy",  
  29.             "Jamaica",  
  30.             "Japan",  
  31.             "Jordan",  
  32.             "Kazakhstan",  
  33.             "Kenya",  
  34.             "Kiribati",  
  35.             "Kuwait",  
  36.             "Kyrgyzstan",  
  37.             "Laos",  
  38.             "Latvia",  
  39.             "Lebanon",  
  40.             "Lesotho",  
  41.             "Liberia",  
  42.             "Libya",  
  43.             "Liechtenstein",  
  44.             "Lithuania",  
  45.             "Luxembourg",  
  46.             "Macau",  
  47.             "Madagascar",  
  48.             "Malawi",  
  49.             "Malaysia",  
  50.             "Maldives",  
  51.             "Mali",  
  52.             "Malta",  
  53.             "Marshall Islands",  
  54.         };  
  55.         // write AutoCompleteTextView code between thw OnCreate() method   
  56.         protected override void OnCreate(Bundle bundle) {  
  57.             base.OnCreate(bundle);  
  58.             // Set our view from the "main" layout resource   
  59.             SetContentView(Resource.Layout.Main);  
  60.             AutoCompleteTextView textView = FindViewById < AutoCompleteTextView > (Resource.Id.autoCompleteTextView1);  
  61.             var adapter = new ArrayAdapter < String > (this, Resource.Layout.list, COUNTRIES);  
  62.             textView.Adapter = adapter;  
  63.         }  


Step 13 - If you have Android virtual device, you can run the virtual device. if you don't have it, run your Android phone.

Connect Android phone, via USB cable in your system or laptop. If you connect with Andriod mobile phone, it shows the message Allow USB debugging?

If you run the app in the mobile, always check allow from this computer. Click OK button.

Go to Visual Studio.

If you connect your Andriod phone, it shows the run menu (Ex:LENOVO A6020a40(Android 5.1-API 22)). It shows the phone in run option.

Go to "click the connected Android phone" and it will run in your Android phone.



Output

In this, the app runs, which may take sometime because the app is built in to your phone and is installed in your phone.
It will take some time.

After running your app in your phone, it shows the app with Auto Suggest Box.

Type "i" show the "i" starting values.



Summary - This was the process to create an Auto Suggest box in Xamarin Android app, using Visual Studio 2015 Update 3.

 


Similar Articles