Juozas Kontvainis
Juozas Kontvainis

Reputation: 9597

Event when searchview expands from iconified view

What event(s) I should listen to determine when user clicks on iconified SearchView. I want to remove some items (ActionBar navigation tabs, if that is important) from action bar to make more space in portrait orientation.

I've tried OnClickListener, OnFocusChangeListener, OnTouchListener and other events but neither gets triggered by SearchView expansion.

Upvotes: 13

Views: 11375

Answers (3)

Eric B.
Eric B.

Reputation: 703

If you're using MenuItemCompat:

MenuItem searchMenuItem = menu.findItem(R.id.action_search);
MenuItemCompat.setOnActionExpandListener(searchMenuItem, new MenuItemCompat.OnActionExpandListener() {
    @Override
    public boolean onMenuItemActionCollapse(MenuItem item) {
        Log.d("TAG", "Collapsed");

        return true;
    }

    @Override
    public boolean onMenuItemActionExpand(MenuItem item) {
        Log.d("TAG", "Expanded");

        return true;
    }
});

Upvotes: 6

Juozas Kontvainis
Juozas Kontvainis

Reputation: 9597

I found a way to get that event using addOnLayoutChangeListener

private final OnLayoutChangeListener _searchExpandHandler = new OnLayoutChangeListener()
    {
    @Override
    public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight,
            int oldBottom)
        {
        SearchView searchView = (SearchView)v;
        if (searchView.isIconfiedByDefault() && !searchView.isIconified())
            {
            // search got expanded from icon to search box, hide tabs to make space
            getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
            }
        }
    };

Upvotes: 14

VeV
VeV

Reputation: 1246

Since API Level 14 you have a dedicated listener: http://developer.android.com/guide/topics/ui/actionbar.html

  @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.options, menu);
        MenuItem menuItem = menu.findItem(R.id.actionItem);
        ...

    menuItem.setOnActionExpandListener(new OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            // Do something when collapsed
            return true;       // Return true to collapse action view
        }
        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            // Do something when expanded
            return true;      // Return true to expand action view
        }
    });
}

Upvotes: 27

Related Questions