AutoComplete And MultiComplete TextView in Mono For Android

Introduction

 
The Android Text Control allows you to configure, style, and manipulate the controls in a variety of ways; we have so many useful attributes we can use within the application to change the style, position, etc. 
 
The four types of Text Control's in Android are:
  1. TextView
  2. EditText
  3. AutoCompleteTextView
  4. MultiCompleteTextView
You have already learned about the first two Text Controls in my previous article (Working With Text Controls in Mono for Android). In this article, I am going to explain the next two Text Controls AutoCompleteTextView and MultiCompleteTextView.
 

AutoCompleteTextView

 
The AutoCompleteTextView is like a text box that can show suggestions based on a list of data that I provide as a source for this field. The list of suggestions is displayed in a drop down menu from which the user can choose an item to replace the content of the edit box with. The drop-down can be dismissed at any time by pressing the back key or, if no item is selected in the drop-down, by pressing the enter/dpad center key. AutoCompleteTextView has an Android:completionThreshold property, to indicate the minimum number of characters a user must enter before the list filtering begins.
 
Lets create AutoCompleteTextView
  1. Start a new project named AutoComplete
     
    create android application
     
  2. Go to the Resources\layout\main.xml file and make changes
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:orientation="vertical"  
    4.     android:layout_width="fill_parent"  
    5.     android:layout_height="wrap_content"  
    6.     >  
    7.   <AutoCompleteTextView android:id="@+id/ACT"  
    8.       android:layout_width="fill_parent"  
    9.       android:layout_height="wrap_content"  
    10.     />  
    11. </LinearLayout> 
    In the above code you can see I create an simple AutoCompleteTextView. 
     
  3. Add a new Android layout inside the project layout folder
     
    add new item in android project
     
    add new layout in android project
     
  4. Open layout1.axml file and make changes, here we create a simple TextView for the items
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <TextView xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:layout_width="fill_parent"  
    4.     android:layout_height="fill_parent"  
    5.     android:textSize="15sp"  
    6.     android:textColor="#000"  
    7. ></TextView> 
  5. Set the Build Action of layout1.axml file to AndroidResource 
     
  6. Now go to the Activity1.cs and create a string array inside the Activity1 class named names and insert values for it
    1. static string[] names = new string[] {"Hello" , "He" ,  "Hell" , "Hewells" ,  "Heat" , "Henrry" ,  "House" , "Hole"};   
  7. At last write the following code below inside the OnCreate() method
    1. AutoCompleteTextView textView = FindViewById<AutoCompleteTextView>(Resource.Id.ACT);  
    2.  var adapter = new ArrayAdapter<String>(this, Resource.Layout.layout1, names);  
    3.  textView.Adapter = adapter; 
    Here is the complete code of Activity.cs
    1. using System;  
    2. using Android.App;  
    3. using Android.Content;  
    4. using Android.Runtime;  
    5. using Android.Views;  
    6. using Android.Widget;  
    7. using Android.OS;  
    8.    
    9. namespace AutoComplete  
    10. {  
    11.     [Activity(Label = "AutoComplete", MainLauncher = true, Icon = "@drawable/icon")]  
    12.     public class Activity1 : Activity  
    13.     {  
    14.         static string[] names = new string[] { "Hello""He""Hell""Hewells""Heat""Henrry""House""Hole" };  
    15.         protected override void OnCreate(Bundle bundle)  
    16.         {  
    17.             base.OnCreate(bundle);  
    18.             SetContentView(Resource.Layout.Main);  
    19.    
    20.             AutoCompleteTextView textView = FindViewById<AutoCompleteTextView>(Resource.Id.ACT);  
    21.             var adapter = new ArrayAdapter<String>(this, Resource.Layout.layout1, names);  
    22.             textView.Adapter = adapter;  
    23.         }  
    24.     }  
    25. }
  8. Run the application, the output looks like below
     
    auto complete textView in android
     
    As you see above AutoComplete will populate the list of suggestions like you enter he than all the matching records will come out in the list. And following you see that if you increase the text like he to hel only two matching records will shows.
     
    AutoCompleteTextView in android
The drawback of using AutoCompleteTextView is that it would try to match the whole sentence not a single word like see the following; in the picture, if you type a second word then no suggestions will be shown for the second word.
 
AotoComplete in Android
 
To overcome this drawback we have another TextControl in Android; it is MultiCompleteTextView.
 

MultiCompleteTextView

 
MultiAutoCompleteTextView works the same way as the AutoCompleteTextView except you can add a Tokenizer that parses the text and allows you to suggest where to start suggesting words. 
 
Lets create MultiCompleteTextView
 
Perform all the steps the same as you did above for AutoCompleteTextView except 2 and 7. So, you need to do the following changes in these two steps:
  • Step 2: Go to the Resources\layout\main.xml file and make changes
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:orientation="vertical"  
    4.     android:layout_width="fill_parent"  
    5.     android:layout_height="wrap_content"  
    6.     >  
    7.   <MultiAutoCompleteTextView android:id="@+id/ACT"  
    8.       android:layout_width="fill_parent"  
    9.       android:layout_height="wrap_content"  
    10.     />  
    11. </LinearLayout> 
  • Step 7: At last write the following code inside the OnCreate() method
    1. MultiAutoCompleteTextView textView = FindViewById<MultiAutoCompleteTextView>(Resource.Id.ACT);  
    2. var adapter = new ArrayAdapter<String>(this, Resource.Layout.layout1, names);  
    3. textView.Adapter = adapter;  
    4. textView.SetTokenizer(new MultiAutoCompleteTextView.CommaTokenizer()); 
    Here is the complete code of Activity.cs for MultiCompleteTextView
    1. using System;  
    2. using Android.App;  
    3. using Android.Content;  
    4. using Android.Runtime;  
    5. using Android.Views;  
    6. using Android.Widget;  
    7. using Android.OS;  
    8.    
    9. namespace MultiAutoComplete  
    10. {  
    11.     [Activity(Label = "AutoComplete", MainLauncher = true, Icon = "@drawable/icon")]  
    12.     public class Activity1 : Activity  
    13.     {  
    14.         static string[] names = new string[] { "Hello""He""Hell""Hewells""Heat""Henrry""House""Hole" };  
    15.         protected override void OnCreate(Bundle bundle)  
    16.         {  
    17.             base.OnCreate(bundle);  
    18.             SetContentView(Resource.Layout.Main);  
    19.    
    20.             MultiAutoCompleteTextView textView = FindViewById<MultiAutoCompleteTextView>(Resource.Id.ACT);  
    21.             var adapter = new ArrayAdapter<String>(this, Resource.Layout.layout1, names);  
    22.             textView.Adapter = adapter;  
    23.             textView.SetTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());  
    24.         }  
    25.     }  
  • Run the application; the output looks like the following; suggestions will be shown for each word
     
    MultiAotoComplete TextView in Android
     
    MultiCompleteTextView in Android
I hope you like this article. Post your comments and feedback so that I can improve myself. 
 
Thank You.......


Similar Articles