public class

AutoCompleteTextField

extends TextField
java.lang.Object
   ↳ com.neomades.ui.View
     ↳ com.neomades.ui.TextEdit
       ↳ com.neomades.ui.TextField
         ↳ com.neomades.ui.AutoCompleteTextField<T>

Class Overview

An editable text field that shows completion suggestions automatically while the user is typing. 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.

The list of suggestions only appears after a given number of characters are typed. This number is defined by the threshold setThreshold(int). The default value is 3 characters.

The suggestions only display when user is typing text. Using methods setText(String) or setText(int) do not display suggestions.

Providing suggestions

The classic way to provide suggestions is to use setData(List). When using this method, all the possible suggestions should be provided.

The following code snippet shows how to create a field which suggests various country names while the user is typing:

 public class CountriesScreen extends Screen {
 	protected void onCreate() {
 		setContentView(R.layout.countries);
 		AutoCompleteTextField field = (AutoCompleteTextField) findView(Res.id.countries_list);
 		field.setData(COUNTRIES);
 	}

 	private static final String[] COUNTRIES = new String[] { "Belgium", "France", "Italy", "Germany", "Spain" };
 }
 

Adapting views

The AutoCompleteTextField can accept complex data types as suggestions with setData(List). Custom views can be shown in the drop-down list to improve the display of each suggestion instead of having plain text only. See setViewAdapter(AutoCompleteViewAdapter).

Text converting

The AutoCompleteTextField can accept complex data types as suggestions with setData(List). The default converter (if no AutoCompleteViewAdapter is present) will call the toString() method on each data object to display it in the drop-down list. To customize the displayed text, a custom AutoCompleteTextConverter can be used with setTextConverter(AutoCompleteTextConverter).

Default filtering

When using setData(List), a default filter is used to show the user only a sub-list of suggestions that best matches the typed text.

This default filter browses the data source to look if the string representation of each element contains a word starting with the current text, ignoring the case. The String representation of the object is obtained through the default or custom AutoCompleteTextConverter.

Custom filtering

Custom filtering can be used to change the way the suggestions are filtered out of the original data. The filter can be applied with setFilter(AutoCompleteFilter). After the text has changed and before the suggestions are presented to the screen, find(String) is called on the custom filter to give an opportunity to return a custom List of suggestions.

A custom filter can also be used if you're going to perform an asynchronous operation (background thread calculations, call a web service). In such a situation, at the end of the operation, simply call setData(List) to apply the suggestions.

Summary

XML Attributes
Attribute Name Related Method Description
autoCorrect setAutoCorrect(boolean) Auto-correct mode  
completionThreshold setThreshold(int) minimum number of characters the user has to type in the edit box before the drop down list is shown  
[Expand]
Inherited XML Attributes
From class com.neomades.ui.TextEdit
From class com.neomades.ui.View
[Expand]
Inherited Constants
From interface com.neomades.ui.Alignment
From interface com.neomades.ui.StretchMode
Public Constructors
AutoCompleteTextField()
Creates an editable text field that shows completion suggestions
Public Methods
void setAutoCorrect(boolean autocorrect)
void setData(List<T> data)
Sets the original data source.
void setFilter(AutoCompleteFilter<T> filter)
Sets a custom filter.
void setSuggestionsInKeyboard(boolean inKeyboard)
Sets if the suggestions should be shown over the keyboard.
void setTextConverter(AutoCompleteTextConverter<T> converter)
Sets how to adapt a data object to String.
void setThreshold(int threshold)

Specifies the minimum number of characters the user has to type in the edit box before the drop down list is shown.

void setViewAdapter(AutoCompleteViewAdapter<T> adapter)
Sets custom views to drop down list.
[Expand]
Inherited Methods
From class com.neomades.ui.TextEdit
From class com.neomades.ui.View
From class java.lang.Object

XML Attributes

autoCorrect

Auto-correct mode

Related Methods

completionThreshold

minimum number of characters the user has to type in the edit box before the drop down list is shown

Related Methods

Public Constructors

public AutoCompleteTextField ()

Creates an editable text field that shows completion suggestions

Public Methods

public void setAutoCorrect (boolean autocorrect)

Overrides setAutoCorrect(boolean). Auto-correct mode displays suggestions on some platforms, which conflicts with auto-complete suggestions. Auto-correct mode will always be deactivated for the autocomplete text field. This setter has no effect.

Related XML Attributes
Parameters
autocorrect true to activate auto-correct, else false

public void setData (List<T> data)

Sets the original data source. When no filter is set with setFilter(AutoCompleteFilter), the default filter is used to present suggestions.

Parameters
data the data to apply on the field (null to remove)

public void setFilter (AutoCompleteFilter<T> filter)

Sets a custom filter.

The custom filter is called every time the text changes. The filter will provide a list of items that matches the user text.

If the filter needs to be made by a web service, simply return an empty list, and call setData(List) when the suggestions list is ready.

public void setSuggestionsInKeyboard (boolean inKeyboard)

Sets if the suggestions should be shown over the keyboard.

Note: This method should be called only once before any data is applied.

Cross-Platform considerations

This method is only working for iOS.

For iOS, by default the suggestions are shown in the keyboard. For other platforms, they are shown under the field.

Parameters
inKeyboard true to show the suggestions over the keyboard, false to show them under the text field

public void setTextConverter (AutoCompleteTextConverter<T> converter)

Sets how to adapt a data object to String. The string is used for filtering results, sorting results, and bind it to drop-down list.

By default, the AutoCompleteTextField will call toString() .

Note: changing the text converter does not reload the drop-down suggestion list.

Parameters
converter the converter to use to convert a data object to text (null to set back the default converter)

public void setThreshold (int threshold)

Specifies the minimum number of characters the user has to type in the edit box before the drop down list is shown.

When threshold is less than or equals 0, a threshold of 1 is applied.

Related XML Attributes
Parameters
threshold the number of characters to type before the drop down is shown

public void setViewAdapter (AutoCompleteViewAdapter<T> adapter)

Sets custom views to drop down list.

Parameters
adapter custom view provider for drop-down list (null to remove).