Android AutoCompleteTextView completes the word based on the reserved words, so no need to write all the characters of the word.
Android AutoCompleteTextView is a editable text field, it displays a list of suggestions in a drop down menu from which user can select only one suggestion or value.
Android AutoCompleteTextView is the subclass of EditText class. The MultiAutoCompleteTextView is the subclass of AutoCompleteTextView class.
There are some methods of AutoCompleteTextView listed below :
Method | Description |
---|---|
getAdapter() | This method returns a filterable list adapter used for auto completion. |
getCompletionHint() | This method returns optional hint text displayed at the bottom of the the matching list. |
getDropDownAnchor() | This method returns the id for the view that the auto-complete drop down list is anchored to. |
getListSelection() | This method returns the position of the dropdown view selection, if there is one. |
isPopupShowing() | This method indicates whether the popup menu is showing. |
setText(CharSequence text, boolean filter) | This method sets text except that it can disable filtering. |
showDropDown() | This method displays the drop down on screen. |
Android MultiAutoCompleteTextView is an editable text view, extending AutoCompleteTextView, that can show completion suggestions for the substring of the text where the user is typing instead of necessarily for the entire thing.
You must must provide a MultiAutoCompleteTextView. Tokenizer to distinguish the various substring like comma (,).
In this example I will show you how to implement AutoComplete and MultiAutoComplete TextView Android.
Let’s get start by creating a project in Eclipse IDE.
1. Create a new project in Eclipse by navigating to File ⇒ New Android ⇒ Application Project and fill required details. (I kept my main activity name as MainActivity.java).
2. Create a layout file for MainActivtiy.java under res ⇒ layout folder. I named the layout file as activity_main.xml. In this layout i had taken one AutoCompleteTextView and one MultiAutoCompleteTextView.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#DDFF" android:orientation="vertical" android:padding="5dp" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:gravity="center" android:text="AutoCompleteTextView" android:textColor="#FF6600" android:textSize="20sp" /> <AutoCompleteTextView android:id="@+id/autoCompleteTextView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:ems="10" android:hint="Enter a key..." /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:gravity="center" android:text="MultiAutoCompleteTextView" android:textColor="#FF6600" android:textSize="20sp" /> <MultiAutoCompleteTextView android:id="@+id/multiAutoCompleteTextView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:hint="Enter a key..." /> </LinearLayout> |
3. Now, go to values directory inside of res directory in that open the strings.xml file and add the following string array for setting items for AutoComplete suggestion keywords.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Android_AutoComplete_n_MultiAutoComplete_TextView_Demo</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <!-- Suggestions items for autocomplete textview --> <string-array name="suggest_items"> <item>Android</item> <item>Java</item> <item>Windows</item> <item>iOS</item> <item>HTML</item> <item>PHP</item> <item>Languages</item> </string-array> </resources> |
4. Now, come to MainActivity.java and add the following code. In this class we just get the string array from strings.xml and add to arraylist and set adapter to autocomplete textview. We have to set the threshold for both textview means to show hints after number of characters entered.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
package com.android_autocomplete_n_multiautocomplete_textview_demo; import java.util.ArrayList; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.MultiAutoCompleteTextView; import android.widget.Toast; public class MainActivity extends ActionBarActivity { private static AutoCompleteTextView autoComplete_textView; private static MultiAutoCompleteTextView multiautoComplete_textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); autoComplete_textView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1); multiautoComplete_textView = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView1); // Calling the suggestions method setSuggestions(); } void setSuggestions() { // Getting the string array from strings.xml String items[] = getResources().getStringArray(R.array.suggest_items); // New Arrays list for storing items ArrayList<String> list = new ArrayList<String>(); for (int i = 0; i < items.length; i++) { // Adding items to arary list list.add(items[i]); } // Adapter for holding the data view ArrayAdapter<String> adapter = new ArrayAdapter<String>( MainActivity.this, android.R.layout.simple_list_item_1, list); // Setting adapter to both textviews autoComplete_textView.setAdapter(adapter); multiautoComplete_textView.setAdapter(adapter); // Specify the minimum type of characters before drop-down list is shown autoComplete_textView.setThreshold(1); multiautoComplete_textView.setThreshold(1); // comma to separate the different items multiautoComplete_textView .setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer()); // When the user clicks an item of the drop-down list an toast will // shown multiautoComplete_textView .setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<> parent, View view, int pos, long id) { Toast.makeText( MainActivity.this, "MultiAutoComplete: " + "you add new item " + parent.getItemAtPosition(pos), Toast.LENGTH_SHORT).show(); } }); } } |
5. Now, run the app and you will get the output as shown in video.
Thanks.
Subscribe to us and get the latest news.