Skip to content Skip to sidebar Skip to footer

How To Disable Autocompletetextview's Drop-down From Showing Up?

I use the following code to set text to an AutoCompleteTextView field. But I noticed that when I set certain text (not all text, but some) to it, it will automatically pop up the d

Solution 1:

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);

Solution 2:

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(newOnTouchListener() {
                    @OverridepublicbooleanonTouch(View v, MotionEvent event) {
            actv.setThreshold(1);
            returnfalse;
        }
    });
    

Solution 3:

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 = newHandler() {
    publicvoidhandleMessage(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!

Solution 4:

You can also enable/disable the drop-down like so:

// disable
actv.setDropDownHeight(0);
// enable
actv.setDropDownHeight(LayoutParams.WRAP_CONTENT);

Solution 5:

setText("someText",false)

false means that it is not filtering

Post a Comment for "How To Disable Autocompletetextview's Drop-down From Showing Up?"