Python Tutorial

Friday, January 14, 2011

Android autocomplete

Android autocompete is very easy. You need to create a AutoCompleteTextView. In AutoCompleteTextView you can set completionThreshold from which character you want to get autocompletion help (here i set 1, means after pressig ist character you will get help for autocomplete).

<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout xmlns:android="" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/selection" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <AutoCompleteTextView android:id="@+id/edit" android:layout_width="150px" android:layout_height="wrap_content" android:completionThreshold="1" /> <TextView android:id="@+id/myTextViev" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="5px" android:layout_y="110px" /> <Button android:id="@+id/myButton" android:layout_width="100px" android:layout_height="50px" android:text="Submit" android:layout_x="5px" android:layout_y="60px" /> </AbsoluteLayout>


import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.TextView;

public class AutoComplete extends Activity implements TextWatcher,
  OnClickListener {
 TextView selection;
 AutoCompleteTextView edit;
 String[] items = { "bangladesh", "india", "pakistan", "australia", "japan",
   "china", "indiaA", "indiaC" };
 Button btn;
 TextView tv;

 public void onCreate(Bundle icicle) {
  selection = (TextView) findViewById(;
  edit = (AutoCompleteTextView) findViewById(;

  btn = (Button) findViewById(;
  tv = (TextView) findViewById(;

  edit.setAdapter(new ArrayAdapter(this,
    android.R.layout.select_dialog_singlechoice, items));

 public void onTextChanged(CharSequence s, int start, int before, int count) {
  // selection.setText(edit.getText());

 public void beforeTextChanged(CharSequence s, int start, int count,
   int after) {
  // needed for interface, but not used

 public void afterTextChanged(Editable s) {
  // needed for interface, but not used


 public void onClick(View v) {
  // TODO Auto-generated method stub
  if (v.getId() == {
   tv.setText("get: "+edit.getText());



Let you want to type bangladesh or bangla or bd, after pressed b you will get all autocomplete help

Image not found

No comments:

Post a Comment