How to completely collapse a SearchView after an item selected?
How to completely collapse a SearchView after an item selected?
I've been struggling with this for weeks.. I have a global search that offers up a custom listview with suggestions as a user types. When a user selects an option, I want the searchview to return to its completely collapsed state.
Instead, it shrinks down but stays in a slightly expanded view.
I've thrown EVERYTHING I can find at this thing to close it, but cannot for the life of me get the right method. Here's the function:
final SearchView.OnCloseListener closeListener = new SearchView.OnCloseListener() { @Override public boolean onClose() { return closeSearch(); } }; protected boolean closeSearch() { _searchView.clearFocus(); _searchView.setQuery("", false); _searchView.setFocusable(false); _searchMenuItem.collapseActionView(); isSearchFragmentVisible(false); return false; }
Close search is then manually called when an item is selected from the custom "suggestion" listview.
Answer by Sebastian for How to completely collapse a SearchView after an item selected?
Found it. The call should be _searchView.onActionViewCollapsed()
. Why on earth you call a "listener" style method (ie, one that begins with 'on') is beyond me.
Answer by siavach for How to completely collapse a SearchView after an item selected?
Instead of calling _searchView.onActionViewCollapsed()
call menuItem.collapseActionView()
where _searchView = menuItem.getActionView()
.
Answer by Ender Muab'Dib for How to completely collapse a SearchView after an item selected?
@sebastian that's not actually right.
I've been stucked at this issue for a while, but finally I've managed to handle it in the right way. You're suppose to call menuSearch.collapseActionView(); instead. This method will call onActionViewCollapsed, which you could override. So you don't call a listener ;)
MenuItem menuSearch = menu.findItem(R.id.itemSearch); SearchView searchView = (SearchView) menuSearch.getActionView(); //Don't use next line //searchView.onActionViewCollapsed(); menuSearch.collapseActionView();
Answer by Bala Vishnu for How to completely collapse a SearchView after an item selected?
This worked for me and it also closed the keyboard by Default
MenuItemCompat.collapseActionView(menuItem);
Answer by TWiStErRob for How to completely collapse a SearchView after an item selected?
Returning false
should have been sufficient based on the latest documentation and the source code iff iconifiedByDefault == true
.
Did you by any chance define iconifiedByDefault="false"
or called setIconifiedByDefault(false)
?
Answer by Stephane for How to completely collapse a SearchView after an item selected?
You should clear the search, remove the focus and then call:
searchView.setIconified(true);
Answer by Michael Zolotarevsky for How to completely collapse a SearchView after an item selected?
I've changed the 'Go' button to 'Next', so the only method that worked for me (on API 19) is typing closeBtn.performClick();
closeBtn.performClick();
(Clicking 'close' button twice) in the OnEditorActionListener
of the AutoCompleteTextView
. You can get the AutoCompleteTextView
like this:
LinearLayout linearLayout1 = (LinearLayout) searchView.getChildAt(0); LinearLayout linearLayout2 = (LinearLayout) linearLayout1.getChildAt(2); LinearLayout linearLayout3 = (LinearLayout) linearLayout2.getChildAt(1); AutoCompleteTextView autoComplete = (AutoCompleteTextView) linearLayout3.getChildAt(0);
Answer by hakobyanheghine for How to completely collapse a SearchView after an item selected?
If you're using Toolbar (android.support.v7.widget.Toolbar) and the corresponding SearchView (android.support.v7.widget.SearchView) with it, this works:
searchView.setQuery("", false); searchView.clearFocus(); searchView.setIconified(true);
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment