Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I cannot get hideSoftInputFromWindow working

Tags:

android

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.

like image 288
fralbo Avatar asked Mar 10 '23 04:03

fralbo


2 Answers

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);
like image 130
surya Avatar answered Mar 20 '23 01:03

surya


Try using the same SearchView object to hide your keyboard

InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(mSearchView.getWindowToken(), 0);
like image 36
swetabh suman Avatar answered Mar 20 '23 00:03

swetabh suman