I search a lot on net but there is nothing about preventing popup menu from closing.
Whenever i click on checkbox item or any other popup menu item, popup menu dismi
In your case R.id.checkbox_item
return false;
This will tell the system that the event has not yet been handeled and it will not dimiss the menu. See HERE
Using popupMenu.show()
to immediately re-show the popup menu does not work correctly with checkable menu items when changing their checked states.
Here a method that prevents closing the popup menu in the first place. Make sure that onMenuItemClick
returns false.
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
item.setChecked(!item.isChecked());
// Do other stuff
// Keep the popup menu open
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
item.setActionView(new View(context));
item.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
return false;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return false;
}
});
return false;
}
});
The trick here is to show the menu right after it dismisses.
Below is a sample code snippet:
popupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
if(item.getItemId()==R.id.search_by_date_checkbox){
item.setChecked(!item.isChecked());
}
//This is the trick here!!!!
popupMenu.show();
return true;
}
});
You can try this trick with your code! This is how I did it. :)
Try declaring the PopupMenu globally and calling popup.show();
before returning true in onMenuItemClick
function.
Oliver's answer above (https://stackoverflow.com/a/31727213/2423194) gave me a crash, and its message told me to use MenuItemCompat
instead. After some tweaking to this code, it works:
// Keep the popup menu open
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
item.setActionView(new View(getContext()));
MenuItemCompat.setOnActionExpandListener(item, new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
return false;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return false;
}
});
Thanks Oliver!
Got it working by adding popup.show(); on the click of the button and at the end of the click.
final ImageButton layerButton = (ImageButton) findViewById(R.id.layers);
final PopupMenu popup = new PopupMenu(MapsActivity.this, layerButton);
popup.getMenuInflater().inflate(R.menu.toolbar_menu, popup.getMenu());
layerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Here
popup.show();
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
handleClicksOnCheckBoxes(item);
return true;
}
});
//And here
popup.show();
}
});
However, this is not an optimal solution because if the list has so many items that it will be possible to scroll the list, the list will be scrolled to the top when clicking on an item.