Jacek Jacek - 4 months ago 8
Android Question

Hide keyboard when AutoComplateTextView item clicked

I'm trying to hide software kayboard when user click on AutoComplateTextView item, but it doesn't work.

This is my code:

mAutoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
/**
* do something
*/
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromInputMethod(mAutoCompleteTextView.getWindowToken(), 0);
}
});

Answer

put this code in the onClick method:

    InputMethodManager inputManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);

    View v = getActivity().getCurrentFocus();

    if (v != null) {

        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

        inputManager.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

    }
Comments