the_prole
the_prole

Reputation: 8945

How to close a SearchView programmatically on back pressed?

I have the same question which I found here which I will re-iterate because the solution is not 100% exactly what I need:

I currently have a SearchView in the action bar of my app. When I click the search icon, the SearchView expands and the keyboard pops up as expected. Clicking the "X" in the SearchView box closes the SearchView as expected. However, when the SearchView is activated and I press the "back" button, my app is exited. This is the correct behavior, but what I am trying to do now is to capture back button press and just have it close the SearchView (not my app) when the SearchView is visible. Is there a way to invoke the SearchView OnCloseListener() programmatically on a back button press? For example, something like this:

Here is the solution:

@Override
public void onBackPressed() {
  if (!searchView.isIconified()) {
    searchView.setIconified(true);
  } else {
    super.onBackPressed();
  }
}

The problem is the solution requires the user to press back not once, but twice: once* to exit the keyboard and **once to close the SearchView.

How can I close the keyboard AND the SearchView at the same time by pressing back once?

edit:

Somone had a similar problem with EditText and the solution was to subclass the EditText view.

Upvotes: 2

Views: 8229

Answers (8)

Kuldeep
Kuldeep

Reputation: 123

@Override
public void onBackPressed() {    
    if (!searchView.isIconified()) {
        searchView.setIconified(true);
        searchView.onActionViewCollapsed()
    } else {
        super.onBackPressed();
    }
}

below method is Clear your text Only

searchView.setIconified(true); 

below This methods is close your search view

searchView.onActionViewCollapsed()

Upvotes: 3

mustofa.id
mustofa.id

Reputation: 354

Instead of hide soft keyboard manually, try clearFocus() method

mSearchView.setIconified(true);
mSearchView.clearFocus();

Upvotes: 0

Damjan Miloshevski
Damjan Miloshevski

Reputation: 1

I did it like this in the activity where the SearchView is located

@Override
    public void onBackPressed() {
        super.onBackPressed();
        if (!searchView.isIconified()) {
            searchView.setIconified(true);
        }
    }

Upvotes: 0

oiyio
oiyio

Reputation: 5925

public class MainActivity extends AppCompatActivity {
    MenuItem menuItemSearch;

    @Override
    protected void onResume() {
      if(menuItemSearch!=null)
        MenuItemCompat.collapseActionView(menuItemSearch);  // while activity begins, searchView is closed if remained open.
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
      menuItemSearch = menu.findItem(R.id.menuSearch);
    }
}

Upvotes: 0

zebraboy
zebraboy

Reputation: 1

An interesting answer!

 @Override
    public void onBackPressed() {
      if (!searchView.isIconified()) {
        searchView.setIconified(true);
        searchView.setIconified(true);
      } else {
        super.onBackPressed();
      }
    }

Upvotes: 0

kammy
kammy

Reputation: 11

Based on @Archana answer, the onBackPressed() should be like :

@Override
public void onBackPressed() {    
    hideSoftKeyboard();
    if (!searchView.isIconified()) {
        searchView.setIconified(true);
    } else {
        super.onBackPressed();
    }
}

private void hideSoftKeyboard(){
    View view = activity.getCurrentFocus ();
    if (view != null) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService (Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow (view.getWindowToken (), 0);
    }
}

or you can also override the onKeyDown()

@Override
public boolean onKeyDown (int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        hideSoftKeyboard ();
        if (! searchView.isIconified ()) {
            searchView.setIconified (true);
        } 
        return true;
    }
    return super.onKeyDown (keyCode, event);
}

private void hideSoftKeyboard() {
    View view = activity.getCurrentFocus ();
    if (view != null) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService (Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow (view.getWindowToken (), 0);
    }
}

Upvotes: 0

Archana
Archana

Reputation: 637

Add a method to close the keyboard within onBackPressed() here is the code to hide keyboard.

private void hideKeyboard() {
InputMethodManager imm = (InputMethodManager) 
    getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mInputField.getWindowToken(), 0);
}

Upvotes: 0

Vishal Sanghani
Vishal Sanghani

Reputation: 894

try

@Override
public void onBackPressed() {

View view = this.getCurrentFocus();
if (view != null) {  
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
if (!searchView.isIconified()) {
    searchView.setIconified(true);
} else {
    super.onBackPressed();
}
}

Upvotes: 0

Related Questions