popup menu with icon - varshaAv/Android-Tutorials GitHub Wiki

Click this link to see how to add popup menu on button click.
Now in this tutorial we will see, how to add icons in popup menu. because, with the help of previous tutorial we can only implement text based popup menu, we cant add icons to it.

but to achieve it, we just need to make little bit changes.

  1. we need to ass one line in res/menu/button_menu.xml
    so, our button_menu.xml file will look like this,

now , make changes in activity.java file, where we have inflared our popup menu,

    public void showMenu(View v,ImageView overflowImageView)
    {
    //with icon
    PopupMenu menu = new PopupMenu(this, overflowImageView);
    menu.inflate(R.menu.note_menu);

    MenuPopupHelper menuHelper = new MenuPopupHelper(this, (MenuBuilder) menu.getMenu(), overflowImageView);
    menuHelper.setForceShowIcon(true);
    menuHelper.show();
    }

Now , navigate to the onClick(), and make following changes...

    @Override
    public void onClick(View view) {
    if(view==menu)
    {
     showMenu(view,youranyimg);//here, "youranyimg" is any initialised ImageView icon
    }                          
⚠️ **GitHub.com Fallback** ⚠️