Android Interview Questions - SPLessons

Android AutoCompleteTextView

Home > Lesson > Chapter 11
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Android AutoCompleteTextView

Android AutoCompleteTextView

Android AutoCompleteTextView automatically shows a list of suggestions in the drop down menu while the user is typing. The user can choose a element from the drop down box to be replaced with the content of edit box. The suggestions are obtained from the data adapter and can be seen only after entering a certain number of characters defined by the threshold. The drop down box can be dismissed at any time by pressing the back key. If no element is selected from the drop down list, you can close it by pressing the enter key. Android AutoCompleteTextView is the subclass of EditText class and the MultiAutoCompleteTextView is the subclass of AutoCompleteTextView class. AutoCompleteTextView can be used exactly the same way as EditText in your layout (as it’s a subclass of EditText). You just need to replace your EditText by an AutoCompleteTextView. For static data, you can use a default array adapter. Ex:
AutoCompleteTextView text=(AutoCompleteTextView)findViewById(R.id.autotext);
//Creating the instance of ArrayAdapter containing list of language names
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.select_dialog_item,listR);
text.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView
In the case of dynamic data, you should create an ArrayAdapter with a special Filter. The input will change everytime when you call performFiltering() method and we retrieve the corresponding values from the webservice. These values are then sent through the FilterResults object to the publishResults() method where we replace the existing elements with the new ones. For Multiple selection (separated by comma) you can use MultiAutoCompleteTextView by setting commaTokenizer(). Ex:
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_dropdown_item_1line, listR);
MultiAutoCompleteTextView textView = (MultiAutoCompleteTextView) findViewById(R.id.edit); textView.setAdapter(adapter);
textView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
The following example illustrates how to use AutoCompleteTextView with a Filter (I have used static data). Create MainActivity.java under src/<your packagename>. MainActivity.java: [java] public class MainActivity extends Activity { Context context; ArrayList<String> listR; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); AutoCompleteTextView text=(AutoCompleteTextView)findViewById(R.id.autotext); context=this; text.setAdapter(new AutocompleteAdapter(getApplicationContext(), R.layout.list_item)); text.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String data= (String) parent.getItemAtPosition(position); Toast.makeText(context, "You clicked on"+" "+data, Toast.LENGTH_SHORT).show(); } }); listR=new ArrayList<String>(); listR.add("Sacrifice"); listR.add("sadness"); listR.add("sandwich"); listR.add("salesman"); /* //Creating the instance of ArrayAdapter containing list of language names ArrayAdapter<String> adapter = new ArrayAdapter<String> (this,android.R.layout.select_dialog_item,listR); text.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView */ } class AutocompleteAdapter extends ArrayAdapter<String> implements Filterable { private ArrayList<String> result; public AutocompleteAdapter(Context context, int textViewResourceId) { super(context, textViewResourceId); } @Override public int getCount() { return result.size(); } @Override public String getItem(int index) { return result.get(index); } @Override public Filter getFilter() { Filter filter = new Filter() { @Override protected FilterResults performFiltering(CharSequence constraint) { FilterResults filterResults = new FilterResults(); if (constraint != null) { // Retrieve the autocomplete results. result = listR; // Assign the data to the FilterResults filterResults.values = result; filterResults.count = result.size(); } return filterResults; } @Override protected void publishResults(CharSequence constraint, FilterResults results) { if (results != null && results.count > 0) { notifyDataSetChanged(); } else { notifyDataSetInvalidated(); } } }; return filter; } } } [/java] Create activity_main.xml under res/layout folder. activity_main.xml: [code lang="xml"] <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFFFFF"> <AutoCompleteTextView android:id="@+id/autotext" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginTop="140dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_weight="1" android:ems="10" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="22dp" android:textColor="#009ACD" android:text="AutoCompleteTextView Example" /> </RelativeLayout> [/code] Create list_item.xml under res/layout folder. list_item.xml: [code lang="xml"] <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#FFFFFF" android:textColor="#000000" android:textSize="20sp" /> [/code] AndroidManifest.xml: [code lang="xml"] <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.spl.myproject" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.spl.myproject.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> [/code] Output: