19

I have an activity where there is an EditText and on enter key search results are shown, so what I simply want to do is to close the keyboard when search results are about to show to prevent the user from having to do it. However if the user wants to refine his search the keyboard should open back up if he taps into the EditText again.

This has been more difficult than I imagined, I've been search and tried a few things most don't even close the keyboard on my HTC, one method where the InputType is set to INPUT_NULL closes the keyboard but it doesn't open afterwards.

Any suggestions on how to do this?

Janusz
  • 176,216
  • 111
  • 293
  • 365
Emil Davtyan
  • 12,725
  • 5
  • 40
  • 62
  • 1
    possible duplicate of [Close/hide the Android Soft Keyboard](http://stackoverflow.com/questions/1109022/close-hide-the-android-soft-keyboard) – Mridang Agarwalla Mar 03 '14 at 07:16
  • Don't think it's a duplicate, this problem is about hiding the keyboard after pressing the search button on keyboard. Just knowing the code for how to hide it doesn't help, since you don't know where to put that code. – Ricardo A. May 21 '19 at 13:33

3 Answers3

22
@Override
public boolean onQueryTextSubmit(String query) {
    // Your search methods

    searchView.clearFocus();
    return true;
}

Straight to the point and clean.

dazito
  • 6,832
  • 11
  • 63
  • 107
  • Problem with this is onQueryTextSubmit is not called when the search edittext is empty. So if you are looking for a way to dismiss the keyboard even when the search field is empty this will not work. – lostintranslation Apr 05 '19 at 16:26
12

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 ...
    }
    
kravemir
  • 9,380
  • 15
  • 54
  • 99
3

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

Gabriel Ittner
  • 1,132
  • 9
  • 22
testingtester
  • 508
  • 4
  • 11