I have a searchView in the ActionBar. I want to dismiss the keyboard when the user is done with input. I have the following queryTextListener on the searchView
final SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String newText) {
// Do something
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
showProgress();
// Do stuff, make async call
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
return true;
}
};
Based on similar questions, the following code should dismiss the keyboard, but it doesn't work in this case:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
I've also tried:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(searchView.getWindowToken(), 0);
Neither one works. I'm not sure if this is a Honeycomb specific problem or if it's related to the searchView in the ActionBar, or both. Has anyone gotten this working or know why it does not work?
Edit: I added the better solution on top, but also kept the old answer as a reference.
Original Answer: I programmed using a setOnQueryTextListener. When the searchview is hidden the keyboard goes away and then when it is visible again the keyboard does not pop back up.
Below Code is working for me to hide keyboard in Searchview
Why dont you try this? When you touch the X button, you trigger a focus on the searchview no matter what.
Simple, straight to the point and clean:
I was trying to do something similar. I needed to launch the
SearchActivity
from anotherActivity
and have the search term appear on the opened search field when it loaded. I tried all the approaches above but finally (similar to Ridcully's answer above) I set a variable toSearchView
inonCreateOptionsMenu()
and then inonQueryTextSubmit()
calledclearFocus()
on theSearchView
when the user submitted a new search:In a tablet app I'm working on with a dual pane activity, I've wrote only
f.getView().requestFocus(); // f is the target fragment for the search
and that was enough to dismiss the soft keyboard after a search. No need to use InputMethodManager