Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

searchview not closing correctly on FIRST back press (it's only loosing focus)

I have to press the backbutton twice, to close the SearchView. Why? On the first press, the SearchViewonly looses focus...

Setting setOnKeyListener on SearchView does not work either...

Btw, I'm using the ABS implementation...

My code is simple and looks like the following:

mMenuItemSearch = menu.findItem(R.id.search);
mSearchView = new SearchView(getSupportActionBar().getThemedContext());
mMenuItemSearch.setActionView(mSearchView);

mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
{
    public boolean onQueryTextChange(String newText)
    {
        mPagerManager.getFragment(mSelectedPos).adapter.getFilter().filter(newText);
        return true;
    }

    public boolean onQueryTextSubmit(String query)
    {
        return true;
    }
});
like image 380
prom85 Avatar asked Sep 20 '13 19:09

prom85


2 Answers

After trying a lot, I found the solution.

In onQueryTextSubmit(String query) write searchView.clearFocus();

Then in onBackPressed() use below code:

        if (!searchView.isIconified())
        {
            searchView.setIconified(true);
            searchView.onActionViewCollapsed();
        }
        else
        {
            super.onBackPressed();
        }
like image 103
Smeet Avatar answered Oct 17 '22 09:10

Smeet


try this. It worked for me after trying lots of things.

mSearchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
  @Override
  public void onFocusChange(View v, boolean hasFocus) {
    if (!hasFocus) {
      MenuItemCompat.collapseActionView(searchMenu);
    }
  }
});
like image 26
vikoo Avatar answered Oct 17 '22 09:10

vikoo