AutocompleteTextView And MultiAutoCompleteTextView
The document explains the use of AutoCompleteTextView and MultiAutoCompleteTextView in Android development, emphasizing their functionalities for user input suggestions. AutoCompleteTextView displays completion suggestions for single values, while MultiAutoCompleteTextView allows for multiple values and tokenization. Code examples illustrate how to implement both views, set their adapters, and define thresholds for suggestion visibility.
AutoCompleteTextView
• In Android,AutoCompleteTextView is a view i.e similar
to EditText, except that it displays a list of completion
suggestions automatically while the user is typing.
• <AutoCompleteTextView
android:id="@+id/countryTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is an AutoCompleteTextView"/>
• AutoCompleteTextView simpleAutoCompleteTextView = (AutoCompleteTextView)
findViewById(R.id. countryTextView" );
• String AutoCompleteTextViewValue =
simpleAutoCompleteTextView.getText().toString();
3.
Adapter
• ArrayAdapter(Context context,int resource, int
textViewResourceId, T[] objects)
• String[] countryNameList = {"India", "China",
"Australia", "New Zealand", "England",
"Pakistan"};
• AutoCompleteTextView simpleAutoCompleteTextView =
(AutoCompleteTextView) findViewById(R.id.countrytv);
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, countryNameList);
simpleAutoCompleteTextView.setAdapter(adapter);
simpleAutoCompleteTextView.setThreshold(1);//start searching
from 1 character
simpleAutoCompleteTextView.setAdapter(adapter); //set the
adapter for displaying country name list
• setThreshold(int threshold):
•This method is used to set threshold value that help us to start
the searching from a specific character. In this we set int type
value used to specify the threshold . Suppose if set 1 value for
threshold then on the time of typing searching is start from first
character.
• // initiate a MultiAutoCompleteTextView
MultiAutoCompleteTextView simpleMultiAutoCompleteTextView =
(MultiAutoCompleteTextView)
findViewById(R.id.simpleMultiAutoCompleteTextView); // set
threshold value 2 that help us to start the searching from second
character simpleMultiAutoCompleteTextView.setThreshold(2);
8.
•
String[] androidVersionNames ={"Aestro", "Blender",
"CupCake", "Donut", "Eclair", "Froyo", "Gingerbread",
"HoneyComb", "IceCream Sandwich", "Jellibean", "Kitkat",
"Lollipop", "MarshMallow"};
• @Override protected void onCreate(Bundle
savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // initiate a
MultiAutoCompleteTextView MultiAutoCompleteTextView
simpleMultiAutoCompleteTextView =
(MultiAutoCompleteTextView)
findViewById(R.id.simpleMultiAutoCompleteTextView); //
set adapter to fill the data in suggestion list
9.
• ArrayAdapter<String> versionNames= new
ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
androidVersionNames);
simpleMultiAutoCompleteTextView.setAdapter(versionName
s); // set threshold value 1 that help us to start the
searching from first character
simpleMultiAutoCompleteTextView.setThreshold(1); // set
tokenizer that distinguish the various substrings by comma
simpleMultiAutoCompleteTextView.setTokenizer(new
MultiAutoCompleteTextView.CommaTokenizer()); }