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

7

5

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?

Loom answered 24/4, 2013 at 6:19 Comment(0)
C
16

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

mSearchView.clearFocus();
Coincidental answered 6/3, 2014 at 10:14 Comment(1)
Do you know how it is possible to open the keyboard again?Governance
U
4

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;
        }

     });
Underestimate answered 20/8, 2013 at 16:26 Comment(1)
This works for me after trying tens of methods! Thanks very much!Coincidental
L
3

try

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Loree answered 24/4, 2013 at 6:37 Comment(1)
+1. Or just put android:windowSoftInputMode="stateAlwaysHidden" on android manifest if no need to set keyboard hidden manually.Eveliaevelin
C
1

add the below line in the manifest for particular Activity.

 android:windowSoftInputMode="adjustPan|stateHidden"
Cullie answered 24/4, 2013 at 6:24 Comment(2)
For which purpose adjustPan is here? Wouldn't be enough just using stateHidden alone?Dr
by using adjustPan current focus is never obscured by the keyboard .. for more info go here.. developer.android.com/guide/topics/manifest/…Cullie
C
1

simple solution its work for my add to XML:

 android:focusable="false"
Convenance answered 24/10, 2017 at 14:40 Comment(0)
N
0

In Android Manifest:

 android:windowSoftInputMode="adjustPan|stateHidden"

In class open and close the keyboard:

   @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action buttons
      switch(item.getItemId()) {
case R.id.search:
         //TODO Whatever
          search.clearFocus();
         //Open and close the  keyboard
          InputMethodManager imm = (InputMethodManager)MyApplication.getAppContext().getSystemService(
                  Context.INPUT_METHOD_SERVICE);
          imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
          imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
          return true;
Nimbostratus answered 13/11, 2014 at 2:3 Comment(0)
P
0

u just have to use: "object(edittext, searchview, etc)".clearfocus() ;

use it after u generate a search or an action. Example: in the method OnQueryTextListener, after that i use a search. For searchview.

Porous answered 17/3, 2019 at 2:20 Comment(1)
How does this improve on the other answers that already mention that function?Parthenope

© 2022 - 2024 — McMap. All rights reserved.