Weekend
Weekend

Reputation: 1563

How to listen BOTH expand/collapse events of SearchView which is NOT a MenuItem?

The SearchView is a direct child of a horizontal RelativeLayout title bar. When it's a MenuItem, we have setOnActionExpandListener. But it's not a MenuItem now. How do I listen both expand/collapse events of the SearchView?


Edit:

1) Malwinder's SearchView.OnCloseListener only triggers for collapse events, but not for expand events.

2) CollapsibleActionView is a MenuItem-related or "action view"-related interface, and is only called when the associated view serves as a MenuItem action view.

Upvotes: 2

Views: 1691

Answers (2)

李鸿章
李鸿章

Reputation: 373

SearchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus){
                //just expanded, do something...
            }else {
                //just collapsed, do something...
            }
        }
    });

Upvotes: 3

Malwinder Singh
Malwinder Singh

Reputation: 7050

You may use SearchView.OnCloseListener

mSearchView.setOnCloseListener(new OnCloseListener()
    {

        @Override
        public boolean onClose()
        {
            Log.i(TAG, "mSearchView on close ");
            // TODO Auto-generated method stub
            return false;
        }
    });

Edit: SearchView implements CollapsibleActionView and it has methods onActionViewCollapsed and onActionViewExpanded(). You may need to override these methods, like this:

class MySearchView extends SearchView {

         public MySearchView(Context context) {
             super(context);
         }

         @Override
         public void onActionViewExpanded() {
             super.onActionViewExpanded();
         }

         @Override
         public void onActionViewCollapsed() {
             super.onActionViewCollapsed();
         }
     }

Upvotes: 2

Related Questions