Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting PopupMenu menu items programmatically

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();

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();
    }
});

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;
        }
    }

Here is a complete solution with IDs set and handled:

        this.findViewById(R.id.hamburger_menu).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            PopupMenu menu = new PopupMenu(getApplicationContext(), v);
            menu.getMenu().add(Menu.NONE, 1, 1, "Share");
            menu.getMenu().add(Menu.NONE, 2, 2, "Comment");
            menu.show();

            menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {

                    int i = item.getItemId();
                    if (i == 1) {
                        //handle share
                        return true;
                    } else if (i == 2) {
                        //handle comment
                        return true;
                    } else {
                        return false;
                    }
                } 

            });
         }
     });

Note: share and comment are for example, also you can put constants for the numbers 1,2 to make the code more readable.

Also, I put Menu.NONE because I don't care about grouping the menu items. In case you want to make group set group id = 1, 2, etc...


@Voora Tarun gave a good answer, and I based my answer on it: First instead using fake mune.xml resources, I think better approach is to create ids file instead:

ids.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="menuGroup" type="id"/>
    <item name="menu1" type="id"/>
    <item name="menu2" type="id"/>
    <item name="menu3" type="id"/>
</resources>

Then you can do something like that:

private void showPopup(final View anchor) {
        PopupMenu popupMenu = new PopupMenu(anchor.getContext(), anchor);
        popupMenu.getMenu().add(R.id.menuGroup, R.id.menu1, Menu.NONE, "slot1");
        popupMenu.getMenu().add(R.id.menuGroup, R.id.menu2, Menu.NONE,"slot2");
        popupMenu.getMenu().add(R.id.menuGroup, R.id.menu3, Menu.NONE,"slot3");
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                Toast.makeText(anchor.getContext(), item.getTitle() + "clicked", Toast.LENGTH_SHORT).show();
                return true;
            }
        });
        popupMenu.show();
    }

 private void getPopup(final TextView textView, ArrayList<String> arrayList) {
        final PopupMenu popupMenu = new PopupMenu(sContext, textView);
        for (int i = 0; i < arrayList.size(); i++) {
            popupMenu.getMenu().add(i, Menu.FIRST, i, arrayList.get(i));
        }
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                textView.setText(item.getTitle());
                return false;
            }
        });
        popupMenu.show();
    }