0

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).

curiousMind
  • 2,812
  • 1
  • 17
  • 38
Sanjana Nair
  • 2,663
  • 6
  • 26
  • 44

2 Answers2

1

Try this:

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

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

Srijith
  • 1,695
  • 17
  • 29
1

It seems that you would never display both "download" and "cancel download" items together at the same time. So why not make two separate menu layouts, and inflate on the correct one by keeping a state of the download process. If there is an ongoing download, simply inflate the layout with the "cancel download" item and if there is no download taking place, inflate the layout with "download" item.

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/video_download_cancel"
        android:title="Cancel Download"
        android:visible="false"/>
</menu>

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/video_download"
        android:title="Download"/>
</menu>

These were the two menu item layouts.

personViewHolder.video_menu.setOnClickListener(new MyOnClickListener(this));

private final class MyOnClickListener implements View.OnClickListener {
    private final Context mContext;
    private boolean mDownloading = false;
    public MyOnClickListener(Context context) {
        mContext = context;
    }
    @Override
    public void onClick(View v)
    {
        final PopupMenu popup = new PopupMenu(mContext, v);
        if (mDownloading) {
            popup.getMenuInflater().inflate(R.menu.popmenu_canceldownload, popup.getMenu());
        } else {
            popup.getMenuInflater().inflate(R.menu.popmenu_download, popup.getMenu());
        }
        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener()
        {
            @Override
            public boolean onMenuItemClick(MenuItem item)
            {
                if (!mDownloading) {
                    Intent intent = new Intent(mContext, Download_Service.class);
                    intent.putExtra("link", urlstring);
                    mContext.startService(intent);

                    MyOnClickListener.this.mDownloading = true;
                } else {
                    // Cancel download
                    MyOnClickListener.this.mDownloading = false;
                }
                return true;
            }
        });
        popup.show();
    }
}
Selçuk Cihan
  • 1,979
  • 2
  • 17
  • 30