How to Change PopupMenu Text and Background Color and text font?

105 Views Asked by At

PopupMenu with ButtonView

I wrote the following code in the button view.Clicking the button brings up an option popup menu like the image above, I want to change the font, color and background color of this pop up menu.How can I do that?

I have added the following codes to the OnClick button ...

PopupMenu mymenu = new PopupMenu(getApplicationContext(), button2);
mymenu.getMenu().add(Menu.NONE, 0, 0, "Orange");
mymenu.getMenu().add(Menu.NONE, 1, 1, "Apple");
mymenu.getMenu().addSubMenu(Menu.NONE, 2, 2, "Sub Menu");

mymenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener(){
    @Override
    public boolean onMenuItemClick(MenuItem menuItem){
        int id = menuItem.getItemId();
        if (id==0) {
                        SketchwareUtil.showMessage(getApplicationContext(), "Orange");
                } else {
            if (id==1) {
                            SketchwareUtil.showMessage(getApplicationContext(), "Apple");
                    } else {
                if (id==2) {
                                PopupMenu mymenu = new PopupMenu(getApplicationContext(), button2);
                mymenu.getMenu().add(Menu.NONE, 3, 3, "Sub 1");
                mymenu.getMenu().add(Menu.NONE, 4, 4, "Sub 2");
                mymenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener(){
                    @Override
                    public boolean onMenuItemClick(MenuItem menuItem){
                        int id = menuItem.getItemId();
                        if (id==3) {
                                        SketchwareUtil.showMessage(getApplicationContext(), "Sub 1");
                                } else {
                            if (id==4) {
                                            SketchwareUtil.showMessage(getApplicationContext(), "Sub 2");
                                    }
                        }
                        return true;
                    }
                });
                mymenu.show();
                    }
        }
    }
    return true;

}
});
mymenu.show();
1

There are 1 best solutions below

0
Forrest On

instead of adding options like that you need to make your own custom layout and inflate it to your menu. here R.menu.mymeny_layout is your custom layout file.

PopupMenu mymenu = new PopupMenu(getApplicationContext(), button2);
MenuInflater inflater = mymenu.getMenuInflater();
inflater.inflate(R.menu.mymenu_layout, mymenu.getMenu());