Setting PopupMenu menu items programmatically

Try this:

Dynamic_PopUpMenu.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        PopupMenu menu = new PopupMenu(DialogCheckBox.this, v);
        menu.getMenu().add("AGIL");         
        menu.getMenu().add("AGILarasan");
        menu.getMenu().add("Arasan");
        menu.show();
    }
});

Just figured it out; for anyone who runs into this same problem you just do:

popup.getMenu().add(groupId, itemId, order, title);

for each MenuItem you want to add.


Just create the popup menu registering the view the popup will show underneath and use the getMenu() method to add the values. Don't forget to call show();

PopupMenu menu = new PopupMenu(this, view);

menu.getMenu().add("titleRes");
menu.show();

Defines ids in popupmenu.xml

<?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">

        <item
            android:id="@+id/slot1"
            app:showAsAction="ifRoom|withText"
            android:title="Movies"
            android:visible="true"/>
        <item
            android:id="@+id/slot2"
            app:showAsAction="ifRoom|withText"
            android:title="Music"
            android:visible="true"/>
        <item
            android:id="@+id/slot3"
            app:showAsAction="ifRoom|withText"
            android:title="Comedy"
            android:visible="true"/>

</menu>  

PopupMenu popupMenu = new PopupMenu(FullMenuActivity.this, view);                      
popupMenu.setOnMenuItemClickListener(FullMenuActivity.this);
popupMenu.getMenu().add(1, R.id.slot1, 1, "slot1");
popupMenu.getMenu().add(1,R.id.slot2,2,"slot2");
popupMenu.getMenu().add(1,R.id.slot3,3,"slot3");
popupMenu.show();

 @Override
    public boolean onMenuItemClick(MenuItem item) {

        switch (item.getItemId()) {
            case R.id.slot1:
                SessionManager.selected_slot = item.getTitle().toString();
                Toast.makeText(this, "slot1 Clicked", Toast.LENGTH_SHORT).show();
                return true;
            case R.id.slot2:
                SessionManager.selected_slot = item.getTitle().toString();
                Toast.makeText(this, "slot2 Clicked", Toast.LENGTH_SHORT).show();
                return true;
            case R.id.slot3:
                SessionManager.selected_slot = item.getTitle().toString();
                Toast.makeText(this, "slot3 Clicked", Toast.LENGTH_SHORT).show();
                return true;
            default:
                return true;
        }
    }