I want to show some default suggestions in SearchView when User hasn\'t typed anything. I am setting my custom suggestion adapter manually using matrix cursor. I tried setting
You can set an expand/collapse listener on the MenuItem
like this:
menuItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return true;
}
});
This listener was introduced with API level 14, so for backwards compatibility you have to use the v4 support library. If you do then you have to set the expand/collapse listener like this:
MenuItemCompat.setOnActionExpandListener(this.searchItem, new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return true;
}
});
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
vwSearch = (SearchView) menu.findItem(R.id.menu_main_search).getActionView();
vwSearch.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
//Do something on Submit
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
//Do something on change
return true;
}
});
vwSearch.setOnCloseListener(new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
//Do something on collapse Searchview
return false;
}
});
return true;
}
the questioner wants to know how to know when the searchview is expanded. just do this with your searchview (assuming its standalone but you can get a reference to it from menu item if needed):
searchView.setOnSearchClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//search is expanded
}
});
here is for closed situation:
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
// searchview closed
return false;
}
});
Try adding this in AndroidManifest.xml along with your activity for example name of your activity is activity1.
<activity
android:name="com.example.app1.activity1"
android:label="@string/title_activity_one"
android:windowSoftInputMode="adjustPan" >
</activity>
instead of "adjustPan" you might use "adjustResize"