Android AutoCompleteTextView for auto complete texts under an EditText

This example demonstrates how to use the AutoCompleteTextView in Android. AutoCompleteTextView is similar to EditText except that it can show a dropdown list of suggested texts while you type

arrays.xml which contains the suggested texts

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="months">
        <item>January</item>
        <item>February</item>
        <item>March</item>
        <item>April</item>
        <item>May</item>
        <item>June</item>
        <item>July</item>
        <item>August</item>
        <item>September</item>
        <item>October</item>
        <item>November</item>
        <item>December</item>
    </string-array>
</resources>

AutoCompleteTextView view in an Activity layout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.autocompletetextview.MainActivity">

    <AutoCompleteTextView
        android:id="@+id/months"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dip"
        android:layout_marginRight="10dip"
        android:textStyle="bold"
        android:hint="Enter a month"
        android:width="250dip" />
</LinearLayout>
public class MainActivity extends AppCompatActivity {

    AutoCompleteTextView autoCompleteTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String[] months = getResources().getStringArray(R.array.months);
        autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.months);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, months);
        autoCompleteTextView.setAdapter(adapter);
        autoCompleteTextView.setThreshold(1);
    }

}

Search within Codexpedia

Custom Search

Search the entire web

Custom Search