Show AutoComplete Suggestions - Android Example

DOWNLOAD CODE

Related Examples

In this example taking array of strings and showing these strings as Auto suggestions for TextView. After AutoComplete value selection showing selected value in alert.

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.

 

Project Structure :


AutoComplete TextView project sketch

 

File : res/layout/auto_complete_string.xml

 

    
 <?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000000" >

    <TableRow>
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20px"
            android:text="Write month name it will show month in autocomplete"
            android:textColor="#ffffff"
            android:layout_marginLeft="10dip"></TextView>
    </TableRow>		
    <TableRow>
        
        <AutoCompleteTextView
            android:id="@+id/Months"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dip"
            android:layout_marginRight="10dip"
            android:textStyle="bold"
            android:width="250dip" />
        
    </TableRow>

 </TableLayout>
 

 

File : src/AutoCompleteString.java

 

- Taking suggestion strings in an array name item.
- Taking AutoCompleteTextView reference from auto_complete_string.xml file.
- Adding item array to adapter and add adapter to AutoCompleteTextView.

 


import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;

public class AutoCompleteString extends Activity implements  OnItemClickListener, OnItemSelectedListener  {
 
    
	// Initialize variables
	 
	AutoCompleteTextView textView=null;
	private ArrayAdapter<String> adapter;
	
	//These values show in autocomplete
	String item[]={
			  "January", "February", "March", "April",
			  "May", "June", "July", "August",
			  "September", "October", "November", "December"
			};
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		
		setContentView(R.layout.auto_complete_string);
		
		 
		// Initialize AutoCompleteTextView values
		
		    // Get AutoCompleteTextView reference from xml
			textView = (AutoCompleteTextView) findViewById(R.id.Months);
		    
			//Create adapter    
			adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, item);
			
			textView.setThreshold(1);
		    
		   //Set adapter to AutoCompleteTextView
		    textView.setAdapter(adapter);
		    textView.setOnItemSelectedListener(this);
		    textView.setOnItemClickListener(this);
		
	      
	}

	
	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int position,
			long arg3) {
		// TODO Auto-generated method stub
		//Log.d("AutocompleteContacts", "onItemSelected() position " + position);
    }

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
		InputMethodManager imm = (InputMethodManager) getSystemService(
			    INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		
		// Show Alert		
    	Toast.makeText(getBaseContext(), "Position:"+arg2+" Month:"+arg0.getItemAtPosition(arg2),
				Toast.LENGTH_LONG).show();
    	
    	Log.d("AutocompleteContacts", "Position:"+arg2+" Month:"+arg0.getItemAtPosition(arg2));
    	
	}
	
	protected void onResume() {
        super.onResume();
    }
 
    protected void onDestroy() {
        super.onDestroy();
    }
   
}

 

File : File : AndroidManifest.xml

 

 
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidexample.autocompletestring"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="8" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.Light.NoTitleBar.Workaround">
        <activity
            android:name="com.androidexample.autocompletestring.AutoCompleteString"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application> 
    
</manifest>