Search code examples
androidandroid-recyclerviewandroid-menuoverflow-menu

Customizing Overflow menu on a RecyclerView in android


I am following a project similar to this: http://www.javatpoint.com/android-popup-menu-example

I have created overflow menu on each item in my recyclerView. The menu is coming up properly. I have created menu item called Download, and another menu Item called Cancel Download. Something like this:

popup.xml (Inside menu folder)

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/video_download"
        android:title="Download"/>

    <item
        android:id="@+id/video_download_cancel"
        android:title="Cancel Download"
        android:visible="false"/>
</menu>

The pop menu is showing up properly without any issues.

Now the question is that:

OnClick of Download I would like to make the video_download to be hidden and video_download_cancel to be visible.

Is this possible?

Here is the click event for the overflow menu (Three vertical dots) I have created:

personViewHolder.video_menu.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                final PopupMenu popup = new PopupMenu(mContext, v);
                popup.getMenuInflater().inflate(R.menu.popmenu, popup.getMenu());
                popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener()
                {
                    @Override
                    public boolean onMenuItemClick(MenuItem item)
                    {
                        Intent intent = new Intent(mContext, Download_Service.class);
                        intent.putExtra("link", urlstring);
                        mContext.startService(intent);

                        return true;
                    }

                });

                popup.show();
            }
        });

where video_menu is an image(three dot vertical).


Solution

  • Try this:

    popup.getMenu().getItem(0).setVisible(false);
    

    Get the instance of Menu and get the first item and make it visible/invisible.