Android AutoComplete TextBox

Guest Posts coolbhushans

AutoCompleteTextView is an editable text view that shows suggestions automatically while the user is entering characters. The list of suggestions is displayed from which the user can choose an item.
The following procedure describes how to set up an AutoCompleteTextView that provides suggestions from an array, using ArrayAdapter.

Create Project : AutoCompleteBox
res/layout/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="horizontal"
    tools:context=".MainActivity" >

    <TextView
        android:layout_marginTop="10dp"
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center|center_vertical"
        android:text="Country Name" />

    <AutoCompleteTextView
        android:layout_marginTop="10dp"

        android:textColor="#000000"
        android:completionHint="Country Name"
        android:id="@+id/autoCompleteTextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:completionThreshold="1"
        android:ems="10" />

</LinearLayout> 

Create Array countries_array as following in res/values/strings.xml

 <?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">AutoCompleteBox</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>

 <string-array name="countries_array">

 <item>Afghanistan</item>
<item>Albania</item>
<item>Algeria</item>
<item>Andorra</item>
<item>Angola</item>
<item>Antigua and Barbuda</item>
<item>Argentina</item>
<item>Armenia</item>
<item>Aruba</item>
<item>Australia</item>
<item>Austria</item>
<item>Azerbaijan</item>
<item>Bahamas, The</item>
<item>Bahrain</item>
<item>Bangladesh</item>
<item>Barbados</item>
<item>Belarus</item>
<item>Belgium</item>
<item>Belize</item>
<item>Benin</item>
<item>Bhutan</item>
<item>Bolivia</item>

    </string-array>

</resources> 

3) src/MainActivity.java

 package com.autocompletebox;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String[] countries = getResources().getStringArray(R.array.countries_array);
        final AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);

        TextView tv=(TextView) findViewById(R.id.textView1);

        ArrayAdapter adapter = 
                new ArrayAdapter(this, android.R.layout.simple_list_item_1, countries);
        textView.setAdapter(adapter);

        textView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub
                String getContryName=textView.getText().toString().trim();
                Toast.makeText(getApplicationContext(), "You Select  "+getContryName+"  Country", Toast.LENGTH_SHORT).show();  
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

} 

Here, a new ArrayAdapter is initialized to bind each item in the COUNTRIES string array to a TextView that exists in the simplelistitem1 layout (this is a layout provided by Android that provides a standard appearance for text in a list).
Then assign the adapter to the AutoCompleteTextView by calling setAdapter().

<img class="alignnone size-full wp-image-4169" alt="ART10
image1" src=http://www.caveofprogramming.com/uploads/2013/12/a1.png width="263" height="320" />

ART10_image2