Android UI How to - Use MultiAutoCompleteTextView








AutoCompleteTextView control offers suggestions only for the entire text in the text view. If you type a sentence, you don't get suggestions for each word.

You can use the MultiAutoCompleteTextView to provide suggestions as the user types.

The MultiAutoCompleteTextView control requires that you give it a tokenizer that can parse the sentence and tell it whether to start suggesting again.

Example

The following code shows how to use the MultiAutoCompleteTextView.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
/*from w ww .j a  va 2  s  .  co  m*/
    <MultiAutoCompleteTextView
        android:id="@+id/mactv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

Java code

package com.java2s.app;
// w w w .  j  a v a 2s .c  om
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.MultiAutoCompleteTextView;

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MultiAutoCompleteTextView mactv = (MultiAutoCompleteTextView) this.findViewById(R.id.mactv);
        ArrayAdapter<String> aa2 = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,
                new String[]{"English", "Hebrew", "Hindi", "Spanish", "German", "Greek"});

        mactv.setAdapter(aa2);

        mactv.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
    }

}
null

Because of the CommaTokenizer, after a comma is typed into the EditText field, the field will again make suggestions using the array of strings. Any other characters typed in will not trigger the field to make suggestions.

Android provides another tokenizer for e-mail addresses called Rfc822Tokenizer.