How to restrict AutoCompleteTextView dropdown dismiss?
Asked Answered
K

2

8

I am working on a AutoCompleteTextView . I get some results when the users type in the AutoCompleteTextView and these are mandatory to select. But the problem is the drop down automatically dismisses when clicking on anywhere in the srceen. I want to avoid this. Is there any way I can achieve this.

Thanks.

Kathe answered 7/2, 2014 at 10:27 Comment(2)
@Andreyua try this matalamaki.fi/2015/09/07/…. Another way is to call autoTextView.showDropDown(); inside onTouchEvent(MotionEvent event), however, it's not beautiful effect :)Shewmaker
you can keep a variable to track if dropdown is visible or not and implement onTouchEvent in your activity. If dropdown is visible then return true and don't call super else call super. Maybe it will work but its not a good solutionStogy
D
1
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 reflexion in public class CustomAutoCompleteTextView extends AutoCompleteTextView, but - maybe this not a good solution too

Disadvantage answered 5/7, 2016 at 8:57 Comment(0)
A
0

try the code below.

I am using the the AutoCompleteText to auto complete the location where the user is currently in, the locationList is nothing but an array that i wrote in the strings.xml file, so use your own string array here.

  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 validating the text input in the location field, the fixText() method prevent the user from typing text that does not exist in your string array, for instance: if the user type "germany" which does not exist in your string array list, it will be replaced by " " which is an empty string inside your 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();
            }
        }
    }
Arielle answered 9/3, 2015 at 10:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.