I have to press the backbutton twice, to close the SearchView
. Why? On the first press, the SearchView
only 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;
}
});
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();
}
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);
}
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With