Android AutoCompleteTextview Example

What is AutoComplete Textview?

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 with.

Get GITHUB code from Here.

Creating New Project

  1. In Android Studio, go to File New Project and fill all the details required to create a new project. When it prompts to select a default activity, select Blank Activity and proceed.
  2. Open activity_main.xml in which I have add  one AutoCompleteTextview with following basic properties.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_margin="10dp"
    tools:context="com.example.lenovo.autocompletetextview.MainActivity">

    <TextView
        android:id="@+id/tv_country_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="Enter country name :"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="20dp"
        android:textStyle="bold" />

    <AutoCompleteTextView
        android:id="@+id/ac_tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_country_name"
        android:layout_marginTop="5dp" />

</RelativeLayout>

3.  Now open  MainActivity.java and and add the below code. The following code snippet shows how to create a text view which suggests various countries names while the user is typing:

MainActivity.java

package com.example.lenovo.autocompletetextview;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;


public class MainActivity extends AppCompatActivity {

    String[] country = {"Australia", "Albania", "Algeria", "Angola", "Belgium", "Bhutan", "Canada", "China", "India"};

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


        //We can use any of the built_in android layout simple_list_item_1 and select_dialog_item
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, country);

        AutoCompleteTextView ac_tv = (AutoCompleteTextView) findViewById(R.id.ac_tv);

        //Start character for search
        ac_tv.setThreshold(1);

        //Setting adapter
        ac_tv.setAdapter(adapter);

        //Setting textcolor after selection
        ac_tv.setTextColor(Color.BLUE);
    }
}
Now if you run the app you can see the book list will look like this:

 

 

 

 

Leave a Reply