I'm inflating a popup menu in my application. I've created a popmenu xml like below.
Song_popup xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/ToolBarStyle">
<item
android:id="@+id/add_queue"
android:title="Add to queue" />
<item
android:id="@+id/play_next"
android:title="Add to favourite" />
<item
android:id="@+id/add_download"
android:title="Download" />
</menu>
Now i want to remove a item by checking a condition. How can i do this?
Code
PopupMenu popup = new PopupMenu(activity, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.song_popup, popup.getMenu());
popup.show();
You can remove a menu item as below:
Menu m = popup.getMenu();
m.removeItem(m.findItem(R.id.add_queue)); //removes "Add to queue"
Implementation of condition is totally up to you.
and this is how you can handle menu item clicks:
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
if(menuItem.getItemId() == R.id.play_next){
Toast.makeText(YourActivity.this, "Play_next", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
You can do it simpily. ex-
if (popup != null && yourcondition){
MenuItem menuitem= popup.getMenu().getItem(1);
menuitem.setVisible(false);
}
Unless I’m missing something, it should be as simple as e.g.:
if (doNotShowAddQueue) {
final Menu menu = popup.getMenu();
menu.removeItem(R.id.add_queue);
}
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