Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if SearchView is expanded?

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

like image 958
injecteer Avatar asked Jul 17 '15 23:07

injecteer


3 Answers

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

like image 119
Vincent Ngo Avatar answered Oct 18 '22 19:10

Vincent Ngo


searchView.setOnSearchClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //do what you want when search view expended
            }
        });
        searchView.setOnCloseListener(new SearchView.OnCloseListener() {
            @Override
            public boolean onClose() {
                //do what you want  searchview is not expanded
                return false;
            }
        });
like image 29
Uttam Meerwal Avatar answered Oct 18 '22 18:10

Uttam Meerwal


Try this, it will detect when the user clicks on the search icon:

In Kotlin:

    searchView.setOnQueryTextFocusChangeListener { _ , hasFocus ->
        if (hasFocus) {
            // searchView expanded
        } else {
            // searchView not expanded
        }
    }

In Java :

searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            // searchView expanded
        } else {
            // searchView not expanded
        }
    }
});
like image 42
ethiopians Avatar answered Oct 18 '22 18:10

ethiopians