I try to hide the soft keyboard once the search is complete but I don't find why it fails.
I create the search menu like below:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
String[] mSubActionsTitles = getResources().getStringArray(R.array.sub_actions_array);
MenuItem item = menu.add(0, R.id.search, 0, mSubActionsTitles[0]);
searchMenuItem = item;
item.setIcon(android.R.drawable.ic_menu_search);
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
mSearchView = new SearchView(getActivity());
mSearchView.setOnQueryTextListener(this);
mSearchView.setOnCloseListener(this);
mSearchView.setIconifiedByDefault(true);
mSearchView.setQueryHint(getString(R.string.label_tvGivePosition));
item.setActionView(mSearchView);
}
When the text is entered, I start an AsyncTask()
which performs an HTTP query.
Then, we the response is received, I call the delegate where I try to hide the keyboard:
public synchronized void sitesLoadingFinish(Integer result) {
searchMenuItem.collapseActionView();
View view = activity.getCurrentFocus();
view.clearFocus();
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
So searchMenuItem.collapseActionView()
is well collapsed, no problem, but I cannot get the soft keyboard closed.
View view = activity.getCurrentFocus();
view .postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
InputMethodManager keyboard = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.hideSoftInputFromWindow(view, 0);
}
},50);
Try using the same SearchView object to hide your keyboard
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mSearchView.getWindowToken(), 0);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With