Android AutoCompleteTextview Example


          AutoCompleteTextView is an editable control, similar to TextView capable of showing suggestions to the user. This will show the suggestions while the user is typing. List of suggestions will be displayed in a drop down view from which the user can select one.
<AutoCompleteTextView> tag is used to create the AutoCompleteTextView.

One of the important properties of the AutoCompleteTextView is "Threshold". This property is used to decide when the suggestion box will appear. This is the minimum number of characters the user must type to see the suggestions.
To dismiss the suggestion box, the user can either press 'back' button or 'enter' button.

We can program array of words in the application that will be displayed to the user. We can also program the background of the dropdown suggestion box.

Some of the important attributes:
Threshold:
Minimum number of characters the user must type to see the suggestions.
XML attribute is android:completionThreshold and corresponding java method is setThreshold().

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.autocompletetextview.MainActivity"
            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>


Layout xml file:

<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"
    tools:context=".MainActivity" >

    <AutoCompleteTextView
        android:id="@+id/autoCompleteTextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="54dp"
        android:ems="10" />

</RelativeLayout>

Activity class:

package com.example.autocompletetextview;

import android.app.Activity;
import android.os.Bundle;
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;

public class MainActivity extends Activity {

 AutoCompleteTextView autocompletetextview;

 String[] arr = { "1", "2", "3" };

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);
    autocompletetextview = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
       ArrayAdapter<Object> adapter = new ArrayAdapter<Object>(this,
   android.R.layout.simple_dropdown_item_1line, arr);

    autocompletetextview.setThreshold(1);
    autocompletetextview.setAdapter(adapter);
    autocompletetextview.setOnItemClickListener(new OnItemClickListener()           {
  public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,    long arg3) {
                      //Do something
  }
    });
 }

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

Source code of this application:
AutoCompleteTextView.zip






Reactions:

0 comments :

Post a Comment