prom85
prom85

Reputation: 17798

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

Upvotes: 14

Views: 4799

Answers (12)

LGFox
LGFox

Reputation: 326

Try removing collapseActionView from app:showAsAction="collapseActionView|ifRoom" in your menu.xml file as mentioned here.

collapseActionView makes the back button first collapse the search view.

Upvotes: 0

kaushalyap
kaushalyap

Reputation: 13597

If your SearchView is on a fragment like mine, use following snippet in Kotlin

searchView.setOnQueryTextFocusChangeListener { _, hasFocus ->
      if(!hasFocus){

         // iconify the SearchView when the focus is lost
         searchView.isIconified = true
      }
 }

Thanks to @vikoo's answer.

Upvotes: 0

vikoo
vikoo

Reputation: 2519

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

Upvotes: 1

Max Efimov
Max Efimov

Reputation: 21

Need call clearFocus() method for SearchView after search button clicked.
For example:

mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
    @Override
    public boolean onQueryTextSubmit(String query) {
        mSearchView.clearFocus();
        //...
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        return false;
    }
});

Upvotes: 0

Stav Bodik
Stav Bodik

Reputation: 2134

onBackPressed should only clear the focus from search view (SearchAutoComplete) EditText, it is normal behavior.

But if you want to change this behavior , do it (:

    final SearchView.SearchAutoComplete searchAutoComplete = (SearchView.SearchAutoComplete) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
    final MenuItem searchViewItem = menu.findItem(R.id.searchView);

    searchAutoComplete.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(!searchAutoComplete.isFocused()){
                searchViewItem.collapseActionView();
                searchView.onActionViewCollapsed();
                viewManager.dismissSearchDropDownPopupWindow();
            }
        }
    });

Upvotes: 1

arsent
arsent

Reputation: 7183

You'd need to extend SearchView and override dispatchKeyEventPreIme

public class SearchView extends android.support.v7.widget.SearchView {
    public SearchView(Context context) {
        super(context);
    }

    public SearchView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SearchView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean dispatchKeyEventPreIme(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
                onActionViewCollapsed();
            }
        }

        return super.dispatchKeyEventPreIme(event);
    }
}

Upvotes: 0

VVB
VVB

Reputation: 7641

Only way to fix this is using android.support.v7.widget.SearchView instead of android.widget.SearchView

I got it fixed by doing this.

Upvotes: 0

Smeet
Smeet

Reputation: 4116

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

Upvotes: 4

youk gwok
youk gwok

Reputation: 1

try this in your configureSearchView() method, it works for me.

    mSearchView.post(new Runnable() {
        @Override
        public void run() {
            mSearchView.setIconified(false);
        }
    });

Upvotes: 0

powder366
powder366

Reputation: 4421

I solved it this way:

...
public class MyFragment extends Fragment implemented SearchView.OnFocusChangeListener    <----
...

@Override
public void onFocusChange(View v, boolean hasFocus) {
    MenuItem menuItem = menu.findItem(R.id.action_search);
    SearchView sv = (SearchView)MenuItemCompat.getActionView(menuItem);
    if(!hasFocus) {
        sv.onActionViewCollapsed();   <----
        interactionListener.showDrawerToggle(true);    <----
    }
}

...

public void configureSearchView(Menu menu) {
    this.menu = menu;
    MenuItem menuItem = menu.findItem(R.id.action_search);
    SearchView sv = (SearchView)MenuItemCompat.getActionView(menuItem);
    sv.setOnSearchClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            interactionListener.showDrawerToggle(false);
        }
    });

    sv.setOnQueryTextListener(this);
    sv.setOnCloseListener(this);
    sv.setSubmitButtonEnabled(false);
    sv.setIconifiedByDefault(true);
    sv.setOnQueryTextFocusChangeListener(this);     <----

    if(initialQuery != null) {
        sv.setIconified(false);
        menuItem.expandActionView();
        sv.setQuery(initialQuery, true);
    }
}
...

Upvotes: 0

Jannik
Jannik

Reputation: 38

You can call view.requestFocus() to give focus to another view. This way the search view will close with one back press.

Upvotes: -1

Jay Snayder
Jay Snayder

Reputation: 4338

The search view closing on back is the expected behavior and what we have accustomed to. You could override that if you would like by extending upon your own SearchView and/or listening for a onBackPressed() to call finish on your activity that the SearchView lives within.

Upvotes: 0

Related Questions