Posts Tagged AutoCompleteTextView

AutoCompleteTextView in Android

We mostly use search engines like Google for finding anything on the internet.  Doing that you might have noticed that as we start typing , a number of  suggestions drops down. An AutoCompleteTextView works in a similar fashion.

Now, let’s see how this can be implemented in android.

Firstly, let’s look at the XML layout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" > 
    <AutoCompleteTextView
        android:id="@+id/autotv"
        android:layout_width="300dip"
        android:layout_height="wrap_content"
        />
</LinearLayout>

I’ve stored the data for the AutoCompleteTextView  in the String Array named AndroidDesk in an XML file named strings.xml in the res/values folder in my project.

Note: You can also create a String Array in your Activity instead.

The strings.xml file is:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, MyAutoCompleteTextViewActivity!</string>
    <string name="app_name">MyAutoCompleteTextView</string>
    <string-array name="AndroidDesk">
    <item >Top Posts </item>
    <item>Activity in android by Deepthi</item>
    <item >Implicit Intent by Deepthi</item>
    <item >Custom Dialog in Android by Deepthi</item>
    <item >Custom Toast in Android by Deepthi</item>
    <item >Creating Progess Dialog in Android by Deepthi</item>
    <item >Check the usage of Checkbox in Android Activity by Deepthi</item>
    <item >Introducing Android by Deepthi</item>
    <item >Button in Android by Deepthi</item>
    </string-array>
</resources>

The data is provided to the AutoCompleteTextView through an array adapter.

final String[] AndroidDesk= getResources().getStringArray(R.array.AndroidDesk);
 ArrayAdapter<String> My_arr_adapter= new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_dropdown_item_1line,AndroidDesk);

Now I need to create a reference to the AutoCompleteTextView  and then set the adapter.

AutoCompleteTextView My_auto_Cmplt_Tv=(AutoCompleteTextView)findViewById(R.id.autotv);
My_auto_Cmplt_Tv.setThreshold(2);
My_auto_Cmplt_Tv.setAdapter(My_arr_adapter);

Here, setThreshold(2) method is used to make the suggestions appear only after typing two letters of what the user desires. Hope I made it clear. 🙂 If not, don’t worry you will understand it better as you go through the screen shots given at the end.

Now, let’s toast a notification telling what we have clicked in the list of suggestions obtained.

The code for doing that is:

My_auto_Cmplt_Tv.setOnItemClickListener(new OnItemClickListener() {
 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),(CharSequence)arg0.getItemAtPosition(arg2), Toast.LENGTH_LONG).show();
                        }
                });

Now , have look at the Whole code:

package com.deepthi.autocompletetextview;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Toast;
 public class MyAutoCompleteTextViewActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final String[] AndroidDesk= getResources().getStringArray(R.array.AndroidDesk);
        ArrayAdapter<String> My_arr_adapter= new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_dropdown_item_1line,AndroidDesk);
        AutoCompleteTextView My_auto_Cmplt_Tv=(AutoCompleteTextView)findViewById(R.id.autotv);
        My_auto_Cmplt_Tv.setThreshold(2);
        My_auto_Cmplt_Tv.setAdapter(My_arr_adapter);
        My_auto_Cmplt_Tv.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
                                // TODO Auto-generated method stub
 Toast.makeText(getApplicationContext(),(CharSequence)arg0.getItemAtPosition(arg2), Toast.LENGTH_LONG).show();
                        }
                });
    }
}

The Output is shown below:

As the user types at least two letters: 

As the user clicks one of the suggestions in the list:

, , , , , ,

19 Comments