Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to hide the virtual keyboard of SearchView iconfiedbydefault(false)

I have a search view which is set as expanded by default with default search query but i don't want the virtual keyboard.In below code i tried to hide keyboard in onCreateOptionsMenu but still keyboard is visible.

imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    MenuItem item = menu.findItem(R.id.menu_search);
    item.expandActionView();
    mSearchView = (SearchView) item.getActionView();
    mSearchView.setIconifiedByDefault(false);
    mSearchView.setQuery(query, true);
    imm.hideSoftInputFromWindow(mSearchView.getWindowToken(), 0);

I am using sherlock search view widget. any suggestion to hide the virtual keyboard.What i am doing wrong?

like image 408
user1526671 Avatar asked Apr 24 '13 06:04

user1526671


2 Answers

Inspired by Parnit's answer, I've found a better method, which also works and is more beautiful:

mSearchView.clearFocus();
like image 59
Xieyi Avatar answered Oct 08 '22 00:10

Xieyi


Edit: I added the better solution on top, but also kept the old answer as a reference.

 @Override
        public boolean onQueryTextSubmit(String query) {

                  searchView.clearFocus();
            return false;
        }

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.

    //set query change listener
     searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){
        @Override
        public boolean onQueryTextChange(String newText) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean onQueryTextSubmit(String query) {
            /**
             * hides and then unhides search tab to make sure keyboard disappears when query is submitted
             */
                  searchView.setVisibility(View.INVISIBLE);
                  searchView.setVisibility(View.VISIBLE);
            return false;
        }

     });
like image 20
Parnit Avatar answered Oct 08 '22 00:10

Parnit