AutoCompleteTextView is an Editable TextView used for displaying suggestion when user starts typing in the control. Suggestion is displayed in a drop down menu and when user select the item from the drop down list, the selected text is displayed in a TextView. Before starting, I suggest you to read my previous Article.
Let's Start:
Method 1:(using Default Android Resources)
1) Create New Android Application.
2) Drop TextView, AutoCompleteTextView(with id="@+id/autoComplete1") and a Button control(with id="@+id/btn_Submit" and text="Submit") on Layout i.e. Main.axml
Main.axml Source Code:
In Activity1.cs, Create a String Array (named as names) used for Displaying AutoComplete Suggestion and an ArrayAdapter for filling the View i.e. 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 than Display a Toast Message(in other words Please Enter Name!).
Activity1.cs Code:
Preview:
Method 2:(Using Custom Resources)
In above example, We have used SimpleSpinnerItem for displaying the list in AutoCompleteTextView but we can also create Custom Resouces for displaing the Auto Suggested list.
Let's Add a new Layout(named as ListName.axml) and Drop TextView Control on it. Adjust textColor, textSize, padding etc according to your need.
Go to Activity1.cs and in ArrayAdapter, use the Custom Layout Resource which we have created. Rest of the code remains same.
Preview:
Hope you like it. Thanks.
Let's Start:
Method 1:(using Default Android Resources)
1) Create New Android Application.
2) Drop TextView, AutoCompleteTextView(with id="@+id/autoComplete1") and a Button control(with id="@+id/btn_Submit" and text="Submit") on Layout i.e. Main.axml
Main.axml 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>
|
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
{
//Creating Instance of AutoCompleteTextView and Button
AutoCompleteTextView
autoComplete1;
Button btn_Submit;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
//setting the view for the Activity
SetContentView(Resource.Layout.Main);
//Get autoComplete1 AutoCompleteTextView and btn_Hello
Button control from the Main.axml Layout.
autoComplete1 = FindViewById<AutoCompleteTextView>(Resource.Id.autoComplete1);
btn_Submit = FindViewById<Button>(Resource.Id.btn_Submit);
//string array used for displayling AutoComplete Suggestion
var names = new string[] {"Anoop","Arjit","Akshay","Ankit","Rakesh"};
//Use ArrayAdapter for filling the View i.e.
AutoCompleteTextView with the list of names(Array).
//Use SimpleSpinnerItem(Predefined layout in Android
Resources) for displaying the dropdown list
ArrayAdapter
adapter = new ArrayAdapter<string>(this,Android.Resource.Layout.SimpleSpinnerItem,names);
autoComplete1.Adapter = adapter;
btn_Submit.Click += btn_Submit_Click;
}
//btn_Submit Click Event
void btn_Submit_Click(object sender, System.EventArgs e)
{
//Checking if autoComplete1.Text is not empty
if (autoComplete1.Text != "")
{
Toast.MakeText(this, "Name Entered ="
+ autoComplete1.Text, ToastLength.Short).Show();
}
else
{
Toast.MakeText(this, "Please Enter Name!", ToastLength.Short).Show();
}
}
}
}
|
Method 2:(Using Custom Resources)
In above example, We have used SimpleSpinnerItem for displaying the list in AutoCompleteTextView but we can also create Custom Resouces for displaing the Auto Suggested list.
Let's Add a new Layout(named as ListName.axml) and Drop TextView Control on it. Adjust textColor, textSize, padding etc according to your need.
ListName.axml 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" />
|
ArrayAdapter adapter = new ArrayAdapter<string>(this, Resource.Layout.ListName, names);
|
Hope you like it. Thanks.
[Download Source Code via Google Drive]
0 comments:
Post a Comment