Android UI How to - Use AutoCompleteTextView








The AutoCompleteTextView control is a TextView with auto-complete functionality. As you types in the TextView, the control can display suggestions for selection.

Example

The following code shows how to use the AutoCompleteTextView control with XML and with the corresponding code.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
/* w ww .j  a v  a 2s  .  c o  m*/
    <AutoCompleteTextView
        android:id="@+id/myID"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

The Java code.

    /*from  w  ww . j  ava  2  s .  c o m*/
package com.java2s.app;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        AutoCompleteTextView actv = (AutoCompleteTextView) this.findViewById(R.id.myID);

        ArrayAdapter<String> aa = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,
                new String[]{"XML", "CSS", "HTML", "Java",
                        "SQL", "Code"}
        );

        actv.setAdapter(aa);
    }

}
    
null