How to limit the dropdown menu of AutoCompleteTextView?

I am working on an AutoCompleteTextView . I get some results when users type in AutoCompleteTextView and they need to be selected. But the problem is that the dropdown menu is automatically rejected when you click srceen anywhere. I want to avoid this. Is there any way to achieve this.

Thanks.

+8
android drop-down-menu textview dismiss autocompletetextview
source share
2 answers

try the code below.

I use AutoCompleteText to automatically complete the location where the user is located, locationList is nothing more than an array that I wrote in the strings.xml file, so here use your own array of strings.

  locationList = res.getStringArray(R.array.ticketLocation); ArrayAdapter<String> locationAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, locationList); AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.txtCountries); textView.setThreshold(1); textView.setAdapter(locationAdapter); textView.setValidator(new Validator()); textView.setOnFocusChangeListener(new FocusListener()); textView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub TextView ticketLocation = (TextView) view; getTicketLocation = ticketLocation.getText().toString(); } }); 

and below is the code for checking text input in the location field, the fixText () method does not allow the user to enter text that does not exist in your string array, for example: if the user enters "germany", which does not exist in the list of string arrays, he will be replaced by "", which is an empty string inside the edittext input field

  class Validator implements AutoCompleteTextView.Validator { @Override public boolean isValid(CharSequence text) { // Log.v("Test", "Checking if valid: " + text); Arrays.sort(locationList); if (Arrays.binarySearch(locationList, text.toString()) > 0) { return true; } return false; } @Override public CharSequence fixText(CharSequence invalidText) { // Log.v("Test", "Returning fixed text"); /* * I'm just returning an empty string here, so the field will be * blanked, but you could put any kind of action here, like popping * up a dialog? * * Whatever value you return here must be in the list of valid * words. */ return ""; } } class FocusListener implements View.OnFocusChangeListener { @Override public void onFocusChange(View v, boolean hasFocus) { // Log.v("Test", "Focus changed"); if (v.getId() == R.id.txtCountries && !hasFocus) { // Log.v("Test", "Performing validation"); ((AutoCompleteTextView) v).performValidation(); } } } 
0
source share
 private boolean setForceIgnoreOutsideTouchWithReflexion(boolean forceIgnoreOutsideTouch) { try { Method method = android.widget.AutoCompleteTextView.class.getMethod("setForceIgnoreOutsideTouch", boolean.class); method.invoke(this, forceIgnoreOutsideTouch); return true; } catch (Exception e) { return false; } } 

Only reflection in the public class CustomAutoCompleteTextView extends AutoCompleteTextView , but - perhaps this is also not a good solution.

0
source share

All Articles