Using setOnActionExpandListener causes SearchView not to expand. Why? And how can I fix it?
menu.findItem(R.id.action_search).setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return false;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
//TODO
return true;
}
});
Try returning true in onMenuItemActionExpand. Per the comments in OnActionExpandListener:
return true if the item should expand, false if expansion should be
suppressed.
Related
I have implemented a menu item to search a list view. I need to make a view invisible when the menu item is selected. This is easily done with this code in my fragment:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_search:
addButton.setVisibility(View.INVISIBLE);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I can't figure out how to set the visibility back when I am done with the search (I am using SearchView). I tried to use onOptionsMenuClosed (Menu menu) but that is not being called for some reason.
Thanks in advance
Try using setOnActionExpandListener():
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.your_menu, menu);
menu.findItem(R.id.action_search).setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
addButton.setVisibility(View.VISIBLE);
return false; // change to true if `false` wont work for your case
}
});
return super.onCreateOptionsMenu(menu);
}
onMenuItemActionCollapse() will be called when SearchView is collapsed or closed.
I have some code that sets a boolean if the search view is open or not.
MenuItemCompat.setOnActionExpandListener(action_search, new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item){
isSearch = true;
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item){
isSearch = false;
return true;
}
});
It works. I'm happy with the way it responds. However, it it shows as deprecated, the setOnActionExpandListener is crossed out with the warning
android.support.v4.view.MenuItemCompat.setOnActionExpandListener is deprecated
Suggestions?
android.support.v4.view.MenuItemCompat.setOnActionExpandListener is deprecated
Yes MenuItemCompat.setOnActionExpandListener This method was deprecated in API level 26.1.0.
Suggestions?
Use MenuItem.setOnActionExpandListener(MenuItem.OnActionExpandListener) directly.
MenuItemCompat.OnActionExpandListener on this menu item to be notified when the associated action view is expanded or collapsed. The menu item must be configured to expand or collapse its action view using the flag SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW.
SAMPLE CODE
MenuItem item = menu.findItem(R.id.action_order);
item.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
isSearch = true;
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem menuItem) {
isSearch = false;
return true;
}
});
This interface was deprecated in API level 26.1.0.
Instead of MenuItemCompat.OnActionExpandListener
Use MenuItem.OnActionExpandListener directly.
Official Documentation
Sample:
MenuItem menuItemSearch = menu.findItem(R.id.action_search);
menuItemSearch.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
isSearch = true;
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem menuItem) {
isSearch = false;
return true;
}
});
Bonus
setting OnQueryTextListener and OnCloseListener
MenuItem menuItemSearch = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) menuItemSearch.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String s) {
//Do something
return false;
}
#Override
public boolean onQueryTextChange(String s) {
return false;
}
});
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
#Override
public boolean onClose() {
// do something
return false;
}
});
how can I capture the back arrow command in Android SearchView.
need to hide the previously opened list
You can use setOnActionExpandListener of MenuItem
mSearchItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem menuItem) {
//TODO Here you can do your list hiding stuff :D
return true;
}
});
How can I remove the three dots to the right of my SearchView ?
set false in onPrepareOptionsMenu
#Override
public boolean onPrepareOptionsMenu (Menu menu) {
return false;
}
Maybe this helps you
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_activity, menu);
MenuItem searchMI = (MenuItem) menu.findItem(R.id.menu_search);
searchMI.setOnActionExpandListener(new OnActionExpandListener(){
#Override
public boolean onMenuItemActionCollapse(MenuItem menuItem) {
// Hide menu icon
return true;
}
#Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
// Show menu icon
return true;
}
});
return true;
}
You can show or hide icons like this:
menu.findItem(R.id.yourActionId).setVisible(true);
menu.findItem(R.id.yourActionId).setVisible(false);
You need to hide all others menu items one by one...
#Override
public boolean onMenuItemActionExpand (MenuItem item){
menu.getItem(MENU_MAIN_REFRESH).setVisible(false);
menu.getItem(MENU_MAIN_SETTINGS).setVisible(false);
menu.getItem(MENU_MAIN_EXIT).setVisible(false);
return true;
}
I use the ActionBarShellock 4.2. I think the OnCloseListener() would be called when I click the closebutton but no response when i did it.
mSearchView.setOnCloseListener(new OnCloseListener() {
#Override
public boolean onClose() {
Toast.makeText(mContext, "OnCloseListener", 1000).show();
return false;
}
});
I've tried to call the getChildAt(index) to get the closebutton. Then I think it's unsafe cause the SearchView is not my own code. So, how can I capture the close event? Did I do in the wrong way?
thanks in advance.
Just get your menuItem, then setOnActionExpandListener. Then override unimplents methods.
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
//do something on close
// you must return true
return true;
}
Hack done
good luck