In this tutorial we will see how it shows a list of completion suggestions automatically while the user is typing. The AutoCompleteTextView is a view that is similar to EditText(in fact it is a subclass of EditText). Try It Out shows how to uses the AutoCompleteTextView to automatically help users complete the text entry.
TRY IT OUT Using the AutoCompeteTextView
2. Modify the main.xml file located in the res/layout folder as shown here:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <AutoCompleteTextView android:id="@+id/AndroidBooks" android:layout_width="fill_parent" android:layout_height="wrap_content"> </AutoCompleteTextView> </LinearLayout>
3. Add the following statements to AutoCompleteText.java file:
package com.sai.samples.views; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; public class AutoCompleteText extends Activity { String[] androidBooks = { "Hello, Android - Ed Burnette", "Professional Android 2 App Dev - Reto Meier", "Unlocking Android - Frank Ableson", "Android App Development - Blake Meike", "Pro Android 2 - Dave MacLean", "Beginning Android 2 - Mark Murphy", "Android Programming Tutorials - Mark Murphy", "Android Wireless App Development - Lauren Darcey", "Pro Android Games - Vladimir Silva", }; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,androidBooks); AutoCompleteTextView acTextView = (AutoCompleteTextView)findViewById(R.id.AndroidBooks); acTextView.setThreshold(1); acTextView.setAdapter(adapter); } }
4. Press F11 to debug the application on the Android Emulator. The below screen shown a list of matching names appears as you type into the AutoCompleteTextView.
How it Works:
In the MainActivity class, you first create a String array containing a list of presidents’ names:
String[] androidBooks = { "Hello, Android - Ed Burnette", "Professional Android 2 App Dev - Reto Meier", "Unlocking Android - Frank Ableson", "Android App Development - Blake Meike", "Pro Android 2 - Dave MacLean", "Beginning Android 2 - Mark Murphy", "Android Programming Tutorials - Mark Murphy", "Android Wireless App Development - Lauren Darcey", "Pro Android Games - Vladimir Silva", };
The ArrayAdapter object manages the array of strings that will be displayed by the AutoCompleteTextView. In the preceding example, you set the AutoCompleteTextView to display in the simple_dropdown_item_ 1line mode:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,androidBooks);
The setThreshold() method sets the minimum number of characters the user must type before the suggestions
appear as a drop-down menu:
textView.setThreshold(3);
The list of suggestions to display for the AutoCompleteTextView is obtained from the ArrayAdapter object:
textView.setAdapter(adapter);
Categories:
Post a Comment