Similar to the answers above, I found I needed to subclass AutoCompleteTextView. However, because internet is a bit slow where I live, I found that removing the delayed handlers every time the user pressed a key was a problem. So basically if I only ran the filter maybe 500ms after the user stopped typing, it might take seconds for the results to appear, which would make the user annoyed.
So instead, I do not clear the handler every time, and let filtering run every couple hundred ms. My code is much less clean Jan Berkel's. I'm sure you can clean it up a bit I'm too lazy, but performance is better in areas with slow internet IMO.
public class CustomCompleteView extends AutoCompleteTextView{
Handler handler=new Handler();
Runnable r;
boolean cleartogo=false;
boolean pending =false;
CharSequence btext;
int bkeyCode;
public CustomCompleteView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public CustomCompleteView(Context context, AttributeSet attrs)
{
super(context,attrs);
}
public CustomCompleteView(Context context, AttributeSet attrs, int defStyle)
{
super(context,attrs,defStyle);
}
@Override
public void performFiltering(CharSequence text, int keyCode){
if (cleartogo){
cleartogo=false;
Log.d(MainActivity.DTAG,"Going to filter on " + text.toString());
pending=false;
super.performFiltering(btext, bkeyCode);
}
else
{
Log.d(MainActivity.DTAG,"Filtering rejected, too soon");
btext=text;
bkeyCode=keyCode;
if (!pending){
if (r==null)
r=new MyRunnable(this);
//try{handler.removeCallbacks(r);} catch (Exception ex){};
handler.postDelayed(r, 500);
pending=true;}
}
}
private class MyRunnable implements Runnable {
CustomCompleteView bc;
MyRunnable(CustomCompleteView c ) {
this.bc=c;
}
public void run() {
Log.d(MainActivity.DTAG,"Special Runnable running");
cleartogo=true;
bc.performFiltering(btext, bkeyCode);
}
}
}