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:
- set
imeOptions
to"actionSearch"
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(); }});
Hide keyboard when user clicks search. To ensure that keyboard won't show when user minimizes and restores
Activity
you have to remove focus fromEditText
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