How to detect if SearchView is expanded?

injecteer picture injecteer · Jul 18, 2015 · Viewed 10.7k times · Source

I have the menu items like:

<item
    android:id="@+id/action_search"
    android:title="search"
    app:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="always"/>

@Override
public boolean onCreateOptionsMenu( Menu menu ) {
  getMenuInflater().inflate( R.menu.passenger, menu );
  searchItem = menu.findItem( R.id.action_search );
  searchView = (SearchView)MenuItemCompat.getActionView( searchItem );
  searchView.setSubmitButtonEnabled( false );
  return super.onCreateOptionsMenu( menu );
}

I want to check if the searchView is expanded.

I tried searchItem.isActionViewExpanded() and searchView.isHovered() but none of them worked.

What am I missing?

TIA

Answer

Vincent Ngo picture Vincent Ngo · Jul 18, 2015

Try searchItem.isIconified()

Returns the current iconified state of the SearchView.

Returns

true if the SearchView is currently iconified, false if the search field is fully visible.

http://developer.android.com/reference/android/widget/SearchView.html#isIconified%28%29