Sherlock SearchView - Closing or Collapsing the Searchview

6458 views
Skip to first unread message

jacob

unread,
Aug 13, 2012, 8:41:54 PM8/13/12
to actionba...@googlegroups.com
Hi

I am using the latest Sherlock Searchview widget. I have a simple requirement/Use case. 

1. User expands the search view by touching the magnifying glass icon.
2. User types in a search text, Software Key board is visible now. 
3. I accept the text and do some web search. 
4. The web result, I am showing in a pop up AlertDialog with a list box. 
5. At this point, the soft keyboard and the search view is visible in the background. fine.
6. When the user selects an item from the AlertDialog list box, I am closing the AlertDialog. My work is done for search text now.

At this point I would like to close/lower the soft keyboard and collapse the Searchview edit box. I read some some posts about using onOptionsItemSelected() for the edit box and closing it there. I am not sure this is helpful for my requirement.

Is there way send a message/call API to search view widget or edit box or close button within search view to close/collapse them (which also lowers the soft key board) and go back original state with the magnifying glass?  (Result is very much like clicking/touching on the "x" button on the search view widget which does the collapsing and lowering the soft keyboard.)

Thank you for any help!
Jacob

Jake Wharton

unread,
Aug 13, 2012, 8:43:21 PM8/13/12
to actionba...@googlegroups.com
Keep a reference to the MenuItem that contains the search view and call collapseActionView() on it.

---
Jake Wharton
http://about.me/jakewharton

Anderson Konzen

unread,
Aug 14, 2012, 6:36:51 PM8/14/12
to actionba...@googlegroups.com
Hello guys!

This helped me to do some actions when I collapse the search view. In my case I wanted to clear the filter on a filtered list if the user clicked on an item.

The thing is I also want to act when the user presses the app icon (which is set to be "up" by default?), and I couldn't find a way to set a listener. I tried to override onOptionsItemSelected and check for the android.R.id.home item id but with no success.

What I am doing now is to override setOnActionExpandListener, then I can clear the filter when the collapse action is called. Is this the right way to do that?

mSearchMenuItem.setOnActionExpandListener(new OnActionExpandListener() {
           
            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
                // TODO Auto-generated method stub
                return true;
            }
           
            @Override
            public boolean onMenuItemActionCollapse(MenuItem item) {
                mAdapter.getFilter().filter("");
                return true;
            }
        });

Thanks!

Ps.: congrats to Jake, ABS is really cool!

Jake Wharton

unread,
Aug 14, 2012, 6:40:23 PM8/14/12
to actionba...@googlegroups.com
That is the correct way to do it. You will also get notified if the user presses the back button to collapse the view.

jacob

unread,
Aug 15, 2012, 12:25:40 AM8/15/12
to actionba...@googlegroups.com
Thank you all for the response. appreciate it. I haven't tried the suggestions yet. Will get to it soon.

- Jacob.

Biggy Yoshi

unread,
Feb 21, 2013, 4:20:23 AM2/21/13
to actionba...@googlegroups.com
You have saved my day! Thank you!
Reply all
Reply to author
Forward
0 new messages