How to disable AutoCompleteTextView's drop-down from showing up?

Another solution is to clear the focus before setting the text:

mContactTxt.setFocusable(false);
mContactTxt.setFocusableInTouchMode(false);
mContactTxt.setText("");            
mContactTxt.setFocusable(true);
mContactTxt.setFocusableInTouchMode(true);

You can try these steps:

  1. When setting the text, also set the Threshold value to a large value so that the dropdown doesnot come.

     actv.setThreshold(1000);
    
  2. Then override the OnTouch to set the threshold back to 1.

       actv.setOnTouchListener(new OnTouchListener() {
                    @Override
        public boolean onTouch(View v, MotionEvent event) {
            actv.setThreshold(1);
            return false;
        }
    });
    

To answer my own question in case someone had the same problem:

One characteristics of AutoCompleteTextView is that if you change its text programmatically, it will drop down the selection list if the following two conditions are met: 1. It has focus; 2. The list is longer than 30-something items.

This behavior is actually, IMHO, a design flaw. When the program sets text to an AutoCompleteTextView, it would mean that the text is already correct, there is no point to popup the filtered list for user to further choose from.

actv.setText("Tim Hortons"); 
actv.setSelection(0, actv.getText().length()); 
actv.requestFocus(); 
actv.dismissDropDown();    // doesn't help 

In the above code, requestFocus() forces the ACTV to get the focus, and this causes the drop-down to pop up. I tried not to reqeuest focus, instead, I called clearFocus() after setting text. But the behavior is very .... unnatural. dissmissDropdown() doesn't help because .... I don't know, it just doesn't help. So, after much strugle, I came up with this work-around:

  1. When initializing the widget, I remembered the adapter in a class field.
  2. Change the above code to:

    mAdapter = (ArrayAdapter<String>)actv.getAdapter(); // mAdapter is a class field        
    actv.setText("Tim Hortons"); 
    actv.setSelection(0, actv.getText().length()); 
    actv.setAdapter((ArrayAdapter<String>)null); // turn off the adapter
    actv.requestFocus();
    Handler handler = new Handler() {
    public void handleMessage(Message msg) {
        ((AutoCompleteTextView)msg.obj).setAdapter(mAdapter);
        };
    Message msg = mHandler.obtainMessage();
    msg.obj = actv;
    handler.sendMessageDelayed(msg, 200);   // turn it back on after 200ms
    

Here the trick is set the ACTV's adapter to null. Because there is no adapter, of course the system will not pop up the drop-down. But the message will reset the adapter back to the ACTV after the programmed delay of 200ms, and the ACTV will work normally as usual.

This works for me!

Tags:

Android