1

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);
   }
}

2 Answers 2

1

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;
    }
}
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.