Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide keyboard after user searches?

Tags:

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?

like image 948
Emil Davtyan Avatar asked Mar 24 '12 19:03

Emil Davtyan


People also ask

How do I hide my keyboard after typing?

This can be anything you wish. Tap the back button on your Android. It's the left-pointing arrow button at the bottom of the screen, either at the bottom-left or bottom-right corner. The keyboard is now hidden.

How do I hide the soft keyboard on Android after clicking outside EditText?

Ok everyone knows that to hide a keyboard you need to implement: InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm. hideSoftInputFromWindow(getCurrentFocus(). getWindowToken(), 0);


2 Answers

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

    searchView.clearFocus();
    return true;
}

Straight to the point and clean.

like image 196
dazito Avatar answered Nov 03 '22 16:11

dazito


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 ...
    }
    
like image 26
kravemir Avatar answered Nov 03 '22 16:11

kravemir