-1

I have a MenuItem searchItem.

which filters listview results as expected.

But when i press search button(magnifying glass) on soft keyboard keyboard does not hide itself.

What should i do to hide keyboard on click of search button on keyboard

MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.menu_search, menu);
        MenuItem searchItem = menu.findItem(R.id.menu_search);

        SearchView searchView = (SearchView) searchItem.getActionView();

        searchView.setSubmitButtonEnabled(true);
        searchView.setIconified(true);

  SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() 
        {
            @Override
            public boolean onQueryTextChange(String newText) 
            {
                // this is adapter that will be filtered
                if(companyListAdapter != null)
                {
                    companyListAdapter.getFilter().filter(newText);
                }

                return true;
            }

            @Override
            public boolean onQueryTextSubmit(String newText) 
            {

                // this is adapter that will be filtered
                if(companyListAdapter != null)
                {
                    companyListAdapter.getFilter().filter(newText);
                }

                return true;
            }
        };
        searchView.setOnQueryTextListener(queryTextListener);

Please help.

Shashank Degloorkar
  • 2,933
  • 4
  • 30
  • 44

1 Answers1

0

This is how to hide and show the keyboard:

/**
 * Hides the soft keyboard
 */
public void hideSoftKeyboard() {
    if(getCurrentFocus()!=null) {
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    }
}

/**
 * Shows the soft keyboard
 */
public void showSoftKeyboard(View view) {
    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
    view.requestFocus();
    inputMethodManager.showSoftInput(view, 0);
}
Philipp Jahoda
  • 47,594
  • 21
  • 164
  • 175
Yogendra
  • 3,633
  • 1
  • 19
  • 18