AutoCompleteTextView: Remove soft keyboard on back press instead of suggestions AutoCompleteTextView: Remove soft keyboard on back press instead of suggestions android android

AutoCompleteTextView: Remove soft keyboard on back press instead of suggestions


You can achieve that by override-ing onKeyPreIme in your custom AutoCompleteTextView.

public class CustomAutoCompleteTextView extends AutoCompleteTextView {    public CustomAutoCompleteTextView(Context context) {        super(context);    }    public CustomAutoCompleteTextView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public CustomAutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    @Override    public boolean onKeyPreIme(int keyCode, KeyEvent event) {        if (keyCode == KeyEvent.KEYCODE_BACK && isPopupShowing()) {            InputMethodManager inputManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);                        if (inputManager.hideSoftInputFromWindow(findFocus().getWindowToken(),                InputMethodManager.HIDE_NOT_ALWAYS)) {                    return true;            }        }        return super.onKeyPreIme(keyCode, event);    }}


set DismissClickListener like this

 autoCompleteTextView.setOnDismissListener(new AutoCompleteTextView.OnDismissListener() {            @Override            public void onDismiss() {                InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);                in.hideSoftInputFromWindow(getCurrentFocus().getApplicationWindowToken(), 0);            }        });