14

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;
    }
});
prom85
  • 16,896
  • 17
  • 122
  • 242

12 Answers12

4

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();
        }
Smeet
  • 4,036
  • 1
  • 36
  • 47
1

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();
            }
        }
    });
Stav Bodik
  • 2,018
  • 3
  • 18
  • 25
1

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);
    }
  }
});
vikoo
  • 2,451
  • 20
  • 27
0

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.

Jay Snayder
  • 4,298
  • 4
  • 27
  • 53
0

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);
    }
}
...
powder366
  • 4,351
  • 7
  • 47
  • 79
0

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

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

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

I got it fixed by doing this.

VVB
  • 7,363
  • 7
  • 49
  • 83
0

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);
    }
}
arsent
  • 6,975
  • 3
  • 32
  • 31
0

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;
    }
});
Max Efimov
  • 21
  • 5
0

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.

user158
  • 12,852
  • 7
  • 62
  • 94
0

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.

LGFox
  • 306
  • 4
  • 11
-1

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

Pete
  • 57,112
  • 28
  • 117
  • 166
Jannik
  • 36
  • 4
  • Activity has to be closed, not another view focused. – Rachit Mishra Apr 04 '16 at 15:17
  • Hi, when you set the focus to another view the search view loses focus it will get closed automatically. Why would you want to close the activity? One activity has multiple views most of the time like a list of items or a SearchView. Take a look here https://developer.android.com/reference/android/view/View.html – Jannik Aug 24 '16 at 12:49