Sunday, July 4, 2010

Example of AutoCompleteTextView

AutoCompleteTextView is an editable text view that shows completion suggestions automatically while the user is typing. The list of suggestions is displayed in a drop down menu from which the user can choose an item to replace the content of the edit box with.

AutoCompleteTextView

It can be comapred with "Example of MultiAutoCompleteTextView".

main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
<TextView 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="@string/hello"
   />
<AutoCompleteTextView android:id="@+id/myautocomplete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:completionThreshold="1"
/>
</LinearLayout>


AndroidAutoCompleteTextView.java
package com.AndroidAutoCompleteTextView;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class AndroidAutoCompleteTextView extends Activity implements TextWatcher{

AutoCompleteTextView myAutoComplete;
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 savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);

       myAutoComplete = (AutoCompleteTextView)findViewById(R.id.myautocomplete);
      
       myAutoComplete.addTextChangedListener(this);
       myAutoComplete.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, item));
      
   }

@Override
public void afterTextChanged(Editable arg0) {
 // TODO Auto-generated method stub

}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
  int after) {
 // TODO Auto-generated method stub

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
 // TODO Auto-generated method stub

}
}


Related:
- AutoCompleteTextView with dynamic suggested words

6 comments:

Anonymous said...

Hi,

I want to have an autocompletetextview that shows always an extra option if the results matching are less than x. It's for an adding item so I don't need an add button next to the autocompletetextview. I have read all properties and methods and this is not possible because you can't add something to show it always and because you can't know how many items are displayed. I'm trying to override the autocompletetextview.class but I have R.Styleable errors. I know I have to write a resource file but I don't know which values I have to add to the styleable atrribute on this resource file. Could you try to do this new autocompletetextview as one of your examples? Or at least, can you give any better idea to do this? I'm quite lost.

Thank you in advance.

Erik said...

hi,

Actually I can't get your point, sorry! Anyway I do a new exercise to have AutoCompleteTextView with dynamic suggested words, please read it here.

Anonymous said...

ok, I will try to explain in a different way.

Imagine you have a database and you want that people add different names. For do that you can create and edittext and a button. My idea is that I can get the different names that my database have and put them in an array and display them as suggestions so the user doesn't need to write all the name. When the user touch an item of the suggestion, the name is added. This works very well. Now my idea is that it will be nice to delete the add button and replace it as an item in the suggestion list. Of course, this item won't be util unless there're 2 or 3 suggestions. That's why I would like to create an item that only displays when there are less than 2 or 3 results in the suggestion list and that do not depend on the user text for appear there.

I have though in override the AutoCompleteTextView to try to do this or to drop a new droplist down the AutoCompleteTextView suggestion list. I think the second one is easier but I'm not sure how to cretae it and have it down the suggestion droplist. I could have the number of suggestions getting the same code of AutoCompleteTextView.java so I need the visual part of create a droplist down the suggestion droplist.

Anonymous said...

Thanks! You helped me.

patelsandip2601 said...

i want to search location on google map by using autocomplete Textview

Anonymous said...

Is text change listener really required ?
Does Autocomplete not do it AutoMatically ?