Forum Xamarin.Android

Popup Menu on Contextual Action Bar its not working

Hello everyone my name is Taniguchi and i created a recyclerview and in this recyclerview i inserted a contextual action bar. And i want to insert a popup menu when i click in one of the items on the actio bar. I followed this documentation:Show popup menu on ActionBar item click but its not working Line: View menuItemView = Activity.FindViewById(Resource.Id.itemTwoId); is showing the error CS0120 and the Line : PopupMenu popupMenu = new PopupMenu(this,menuItemView); is showing the error : CS1503

My contextual action bar:

public class MyActionMode : Java.Lang.Object, ActionMode.ICallback
{

    private RecyclerViewHolder holder;
    private Context mContext;
    private RecyclerView.Adapter mAdapter;
    private int currentPosition;
    private IMenu menu;

    public MyActionMode(Context context) : this(context, null, 0)
    {

    }

    public MyActionMode(Context context, RecyclerView.Adapter adapter, int position)
    {
        mContext = context;
        mAdapter = adapter;
        currentPosition = position;
    }

    public bool OnActionItemClicked(ActionMode mode, IMenuItem item)
    {

        switch (item.ItemId)
        {
            case Resource.Id.itemOneId:
                View menuItemView = Activity.FindViewById<View>(Resource.Id.itemTwoId); // SAME ID AS MENU ID
                PopupMenu popupMenu = new PopupMenu(this,menuItemView);
                popupMenu.Inflate(Resource.Menu.popup_menu);
                // And define click listener here.
                popupMenu.Show();
                return true;
            case Resource.Id.itemTwoId:
                // do Update
                return true;
            default:
                return false;
        }
    }

    public bool OnCreateActionMode(ActionMode mode, IMenu menu)
    {
        mode.MenuInflater.Inflate(Resource.Menu.ContextualMenu, menu);

        return true;
    }

    public void OnDestroyActionMode(ActionMode mode)
    {
        mode.Dispose();
    }

    public bool OnPrepareActionMode(ActionMode mode, IMenu menu)
    {
        return false;
    }
}

the error appears only OnActionItemClicked function.

Best Answer

  • TaniguchiSantosTaniguchiSantos Member ✭✭✭
    Accepted Answer

    Thanks for answering @Jarvan
    I ve found my answer to how to insert a popup menu in contextual action bar.

    Firstly, you need to change your Contextualmenu, change item actionviewclass as android.widget.Button.

    <item android:id="@+id/itemOneId" android:title="Delete"/> Secondly, fire this button click event in OnCreateActionMode public bool OnCreateActionMode(ActionMode mode, IMenu menu) { mode.MenuInflater.Inflate(Resource.Menu.ContextualMenu, menu); button =(Button)menu.FindItem(Resource.Id.itemTwoId).ActionView; button.Background = null; button.Text = "UPDATE"; button.Click += delegate { PopupMenu menu1 = new PopupMenu(mContext, button); menu1.Inflate(Resource.Menu.popup_menu); menu1.Show(); }; return true; }

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai

    View menuItemView = Activity.FindViewById(Resource.Id.itemTwoId); is showing the error CS0120 and the Line

    Compiler Error CS0120: An object reference is required for the nonstatic field, method, or property 'member'. In order to use a non-static field, method, or property, you must first create an object instance.
    Tutorial: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs0120

    Create an instance of 'Activity'.

    var activity = mContext as Activity;
    View view = activity.FindViewById<View>(Resource.Id.itemTwoId); // SAME ID AS MENU ID
    PopupMenu popupMenu = new PopupMenu(mContext, view);
    popupMenu.Inflate(Resource.Menu.popup_menu);
    
    popupMenu.Show();
    
  • TaniguchiSantosTaniguchiSantos Member ✭✭✭
    Accepted Answer

    Thanks for answering @Jarvan
    I ve found my answer to how to insert a popup menu in contextual action bar.

    Firstly, you need to change your Contextualmenu, change item actionviewclass as android.widget.Button.

    <item android:id="@+id/itemOneId" android:title="Delete"/> Secondly, fire this button click event in OnCreateActionMode public bool OnCreateActionMode(ActionMode mode, IMenu menu) { mode.MenuInflater.Inflate(Resource.Menu.ContextualMenu, menu); button =(Button)menu.FindItem(Resource.Id.itemTwoId).ActionView; button.Background = null; button.Text = "UPDATE"; button.Click += delegate { PopupMenu menu1 = new PopupMenu(mContext, button); menu1.Inflate(Resource.Menu.popup_menu); menu1.Show(); }; return true; }
Sign In or Register to comment.