Skip to content

Latest commit

 

History

History
77 lines (59 loc) · 3.25 KB

AutoCompleteTextView.workbook

File metadata and controls

77 lines (59 loc) · 3.25 KB
uti platform packages
com.xamarin.workbook
Android

AutoCompleteTextView

This guide will demonstrate how to use an AutoCompleteTextView in an Android application. An AutoCompleteTextView will display a list of suggestions that match the text a user has entered into a TextView. An AutoCompleteTextView doesn’t work alone - it requires the assistance of another widget which must extend Android.Widget.BaseAdapter. The adapter is a special class that will display a individual item from a list in the drop down list UI that is presented by the AutoCompleteTextView.

First, let’s import the necessary namespaces that we need, and declare a list hat holds the names of some monkeys.

using System;
using Android.App;
using Android.Util;
using Android.Views;
using Android.Widget;

// Get a reference to the Activity.
Activity activity = StartedActivities.First();

// A small list of some different types of monkeys
List<String> allTheMonkeys = new List<String> { "Allen's swamp monkey",
  "Black capuchin", "Black-headed marmoset", "Black-headed night monkey", "Black-shanked douc", 
  "Cotton top tamarin",
  "Drill",
  "Gee's golden lemur", "Geoffrey's tamarin", "Gelada", "Golden lion Tamarin", "Grivet", 
  "Japanese macaque", 
  "Lesula", 
  "Mandril", 
  "Yellow baboon"
};

We will need to create an ArrayAdapter<String> and assign it to the AutoTextCompleteTextView widget. In this example, each row in the AutoCompleteTextView will use a built-in Android layout, the Android.Resource.Layout.SimpleListItem1:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity, Android.Resource.Layout.SimpleListItem1, allTheMonkeys);
adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);

AutoCompleteTextView actv = new AutoCompleteTextView(activity);
actv.Adapter = adapter;

In the next snippet, we add the actv to the Activity, want to add some padding to the views. Typically, one uses device-independent pixels (DPs) for positioning views on an Android layout. However, the SetPadding method takes pixels. The DP2Pixel method will take a DP value and convert that to pixels appropriate to the device.

int DP2Pixel(int dpValue)
{
    float density = activity.Resources.DisplayMetrics.Density;
    int paddingDP = (int) (dpValue * density);
    return paddingDP; 
}

int paddingLeft = DP2Pixel(10);
int paddingRight = paddingLeft;
int paddingTop = DP2Pixel(20);
int paddingBottom = DP2Pixel(9);

actv.SetPadding(paddingLeft, 0, paddingRight, 0);

LinearLayout rootLayout = new LinearLayout(activity);
TextView label = new TextView(activity);
label.Text = "Pick a Monkey:";
label.SetPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);

rootLayout.Orientation = Orientation.Vertical;
rootLayout.AddView(label);
rootLayout.AddView(actv);

activity.SetContentView(rootLayout);

Further Reading

You may want to check out the ListView and the Spinner workbooks as they are very similar to the AutoCompleteTextView control. To display more complex data, such as a POCO, it is better to create a custom Adapter in C#; you can learn more about this by consulting the Xamarin ListView & Adapters guide.