user1017324
user1017324

Reputation: 11

How to use ContextMenu in Monodroid

I am trying to implement a ContextMenu from a button in the titlebar, but it doesn't appear to be working. I register the button, but when I click it, nothing is happening. Any ideas? I'm using MonoDroid 1.2. Thanks.

public class MainActivity : Activity
{
   protected override void OnCreate(Bundle bundle)
   {
       base.OnCreate(bundle);

       SetContentView(Resource.Layout.main);

       Button btnMenu =
       (Button)FindViewById(Resource.Id.btn_menu_options);
       RegisterForContextMenu(btnMenu);
   }

   public override void OnCreateContextMenu(IContextMenu menu, View v,
     IContextMenuContextMenuInfo menuInfo)
   {
       base.OnCreateContextMenu(menu, v, menuInfo);

       menu.SetHeaderTitle(Resource.String.menu_title);
       menu.Add(Resource.String.menu_option1);
   }
}

Upvotes: 1

Views: 1931

Answers (2)

om3ga1582
om3ga1582

Reputation: 1

I believe the context menu is a long press. Just pressing the button normally won't activate it. You need to do a long press.

Upvotes: 0

mironych
mironych

Reputation: 2978

What do you mean under "button in the titlebar"? I have just created demo project and all works fine. Here is a code:

[Activity(Label = "MonoAndroidApplication1", MainLauncher = true, Icon = "@drawable/icon")]
public class Activity1 : Activity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.Main);
        var button = FindViewById<Button>(Resource.Id.MyButton);
        RegisterForContextMenu(button);
    }

    public override void OnCreateContextMenu(IContextMenu menu, View v, IContextMenuContextMenuInfo menuInfo)
    {
        base.OnCreateContextMenu(menu, v, menuInfo);
        menu.SetHeaderTitle(Resource.String.MenuTitle);
        menu.Add(Resource.String.Action1);
        menu.Add(Resource.String.Action2);
    }

    public override bool OnContextItemSelected(IMenuItem item)
    {
        Toast.MakeText(this, item.TitleFormatted, ToastLength.Short).Show();
        return true;
    }
}

Upvotes: 1

Related Questions