Use MultiAutoCompleteTextView

Description

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   ww w. ja  v a  2  s  . c  o m
    <MultiAutoCompleteTextView
        android:id="@+id/mactv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

Java code


package com.java2s.app;
// w ww . j a v  a2  s .com
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());
    }

}
Use MultiAutoCompleteTextView

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.





















Home »
  Android »
    Android UI »




UI Basics
Action Bar
Animation
Button
Canvas
CheckBox
Clock Date Picker
Dialog
EditText
Event
Fragment
Gesture
GridView
ImageView
Layout
ListView
Map
Menu
Model
OpenGL
ProgressBar
RadioButton
Spinner
Tab
TextView
Thread
Toast
Video
View
WebView