Hide keyboard after user searches? Hide keyboard after user searches? android android

Hide keyboard after user searches?


@Overridepublic boolean onQueryTextSubmit(String query) {    // Your search methods    searchView.clearFocus();    return true;}

Straight to the point and clean.


The right way to do this:

  1. set imeOptions to "actionSearch"
  2. initialize listeners for input and search button(if provided)

    searchEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {    @Override    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {        if (actionId == EditorInfo.IME_ACTION_SEARCH) {            performSearch();            return true;        }        return false;    }});view.findViewById(R.id.bigSearchBar_button).setOnClickListener(new View.OnClickListener() {    @Override    public void onClick(View v) {        performSearch();    }});
  3. Hide keyboard when user clicks search. To ensure that keyboard won't show when user minimizes and restores Activity you have to remove focus from EditText

    private void performSearch() {    searchEditText.clearFocus();    InputMethodManager in = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);    in.hideSoftInputFromWindow(searchEditText.getWindowToken(), 0);    ... perform search ...}


I belive this code snippet will close the keyboard:

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);

if not try this one:

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

let me know if these work