Android AutoCompleteTextView

By | March 8, 2015

In this Tutorial we will see one of the android view called AutoCompleteTextView and MultiAutoCompleteTextView , It is an editable text view that shows completion suggestions automatically while the user is typing. The list of suggestions is displayed in a drop down menu from which the user can choose an item to replace the content of the edit box.

XML Layout

file : activity_main.xml

<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”
    android:padding=”20dp” >

    <TextView
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”
        android:text=”AutoCompletion”
        android:textSize=”18sp” />

    <AutoCompleteTextView
        android:id=”@+id/autoCompleteTextView1″
        android:layout_width=”match_parent”
        android:layout_height=”wrap_content”
        android:hint=”Enter…” >
    </AutoCompleteTextView>

    <TextView
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”
        android:layout_marginTop=”25dp”
        android:text=”MultiAutoCompletion”
        android:textSize=”18sp” />

    <MultiAutoCompleteTextView
        android:id=”@+id/multiautoCompleteTextView1″
        android:layout_width=”match_parent”
        android:layout_height=”wrap_content”
        android:hint=”Enter…” />

</LinearLayout>

Main Activity

  1. Create a class MainActivty which extends Activity and set the content of this activity with the above defined xml layout (activity_main.xml) file.
  2.  The setThreshold(int) method of autocompeletion specifies after how many char the autocompeletion should work.
  3.  Create An ArrayList and add data to it.
  4.  Create An ArrayAdapter and set this adapter to autocompeletion view
  5.  The setTokenizer() method specifies the separate or delimiter between items inside the edittext box.  

file : MainActivity.java

package com.pavan.autocomp;

import java.util.ArrayList;

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

public class MainActivity extends Activity {

    AutoCompleteTextView atv;
    MultiAutoCompleteTextView matv;

    ArrayList<String> searchArrayList;

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

        // initisearchArrayListize views
        atv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
        matv = (MultiAutoCompleteTextView) findViewById(R.id.multiautoCompleteTextView1);

        // ArrayList
        searchArrayList = new ArrayList<String>();
        // Add String data to Array List
        searchArrayList.add(“United States”);
        searchArrayList.add(“France”);
        searchArrayList.add(“United Kingdom”);
        searchArrayList.add(“India”);
        searchArrayList.add(“Indonesia”);
        searchArrayList.add(“Germany”);
        searchArrayList.add(“Spain”);
        // ArrayAdapter
        ArrayAdapter<String> adapter = new ArrayAdapter<>(
                getApplicationContext(), R.layout.simple_text, searchArrayList);

        atv.setThreshold(1);// starts working from first char
        atv.setAdapter(adapter); // set adapter

        matv.setThreshold(0); // starts working from first char
        matv.setAdapter(adapter);// set adapter
        matv.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());// setToken

    }

}

 AutoCompeletion

MultiAutoCompeletion