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
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
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;
}
});
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
}
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With