Android MultiAutocompleteTextView With Examples

In this tutorial, we’ll be discussing and implementing the MultiAutocompleteTextView widget in our application.

Android MultiAutoCompleteTextView

MultiAutoCompleteTextView extends AutoCompleteTextView. Unlike AutoCompleteTextView which shows the suggestion for only one string, a MultiAutoCompleteTextView shows you suggestions for each of the substrings you enter separated by a token.

This feature is fairly common in places such as specifying multiple tags(Ever came across that on StackOverflow or Github?). You must have used that when sending messages to multiple people too.

An AutoCompleteTextView only offers suggestion about the whole text.

How is it implemented?

A Tokenizer instance is set on the MultiAutoCompleteTextView instance. By default in Android, we have a CommaTokenizer built-in class to separate the auto complete strings by commas. Once a string is selected from the dropdown, the comma is appended to mark the end of that substring.

The Tokenizer is set inside the method setTokenizer().

Another important method: setThreshold() is used to specify the number of characters after which the dropdown with the autocomplete suggestions list would be displayed.

Let’s jump to the coding part.

Project Structure

 With Examples

Code

The code for the activity_main.xml class is given below:

The first MultiAutoCompleteTextView could use a Comma Tokenizer. The second would use a custom one – Space Tokenizer.

The code for the SpaceTokenizer.java is given below:

The methods findTokenStart findTokenEnd and terminateToken are a part of the Tokenizer interface and are implemented.

The code for the MainActivity.java class is given below

The output of the above application in action is given below:

android multi-autocompletetextview

This brings an end to this tutorial. You can download the project source code from the link below.

By admin

Leave a Reply

%d bloggers like this: