Introduction
AutoCompleteTextView is an Editable TextView for displaying suggestions when the user starts typing in the control. The Suggestions are displayed in a drop down menu and when the user selects the item from the drop-down list, the selected text is displayed in a TextView. Before starting, I suggest you read my previous articles in this series.
Let's Start.
Method 1
Using Default Android Resources.
1. Create a New Android Application.
2. Drop a TextView, AutoCompleteTextView (with id="@+id/autoComplete1") and a Button control (with id="@+id/btn_Submit" and text="Submit") onto the Layout, Main.xaml.
The following is the Main.xaml source code:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <TextView
- android:text="Enter Name"
- android:textAppearance="?android:attr/textAppearanceMedium"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/textView1"
- android:padding="5dp" />
- <AutoCompleteTextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/autoComplete1" />
- <Button
- android:text="Submit"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/btn_Submit" />
- </LinearLayout>
In
Activity1.cs, create a String Array (named names) for displaying the AutoComplete Suggestion and an ArrayAdapter for filling in the View, AutoCompleteTextView, with the list of names (Array). We have also created a btn_Submit.Click event. On clicking the btn_Submit it will display the text present in autoComplete1 (AutoCompleteTextView) in a Toast message and if AutoCompleteTextView is empty then Display a Toast Message (in other words Please Enter Name!).
The following is the activity1.cs code:
- using Android.App;
- using Android. Widget;
- using Android.OS;
-
- namespace AutoCompleteTextViewExample
- {
- [Activity(Label = "AutoCompleteTextViewExample", MainLauncher = true, Icon = "@drawable/icon")]
- public class Activity1 : Activity
- {
-
- AutoCompleteTextView autoComplete1;
- Button btn_Submit;
- protected override void OnCreate(Bundle bundle)
- {
- base.OnCreate(bundle);
-
- SetContentView(Resource.Layout.Main);
-
- autoComplete1 = FindViewById<AutoCompleteTextView>(Resource.Id.autoComplete1);
- btn_Submit = FindViewById<Button>(Resource.Id.btn_Submit);
-
-
- var names = new string[] {"Anoop","Arjit","Akshay","Ankit","Rakesh"};
-
-
- ArrayAdapter adapter = new ArrayAdapter<string>(this,Android.Resource.Layout.SimpleSpinnerItem,names);
- autoComplete1.Adapter = adapter;
-
- btn_Submit.Click += btn_Submit_Click;
- }
-
- void btn_Submit_Click(object sender, System.EventArgs e)
- {
-
- if (autoComplete1.Text != "")
- {
- Toast.MakeText(this, "Name Entered =" + autoComplete1.Text, ToastLength.Short).Show();
- }
- else
- {
- Toast.MakeText(this, "Please Enter Name!", ToastLength.Short).Show();
- }
- }
- }
- }
Preview
Method 2
Using Custom Resources.
In the preceding example, we used SimpleSpinnerItem for displaying the list in AutoCompleteTextView but we can also create Custom Resouces for displaying the Auto Suggested list. Let's add a new layout(named ListName.xaml) and drop a TextView Control onto it. Adjust textColor, textSize, padding and so on depending on your needs.
The following is the ListName.xaml code:
- <?xml version="1.0" encoding="utf-8"?>
- <TextView xmlns:android="http://schemas.android.com/apk/res/android"
- android:text="Text"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/textView1"
- android:padding="5dp"
- android:textSize="20dp"
- android:textColor="@android:color/black" />
Go to Activity1.cs and in ArrayAdapter, use the Custom Layout Resource that we created. The other code remains the same.
- ArrayAdapter adapter = new ArrayAdapter<string>(this, Resource.Layout.ListName, names);
Preview
I hope you like this. Thanks.