Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Xamarin Mobile Development for Android Cookbook

You're reading from   Xamarin Mobile Development for Android Cookbook Over 80 hands-on recipes to unleash full potential for Xamarin in development and monetization of feature-packed, real-world Android apps

Arrow left icon
Product type Paperback
Published in Nov 2015
Publisher
ISBN-13 9781784398576
Length 456 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Matthew Leibowitz Matthew Leibowitz
Author Profile Icon Matthew Leibowitz
Matthew Leibowitz
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Preface 1. Working with Xamarin.Android FREE CHAPTER 2. Showing Views and Handling Fragments 3. Managing App Data 4. Presenting App Data 5. Communicating with the Outside World 6. Using Background Tasks 7. Notifying Users 8. Interacting with Other Apps 9. Presenting Multimedia 10. Responding to the User 11. Connecting to Wearables 12. Adding In-App Billing 13. Publishing Apps Index

Creating contextual action mode menu

Some controls or regions in the user interface allow for additional actions to be performed. However, due to limited screen space, these actions need to be hidden until the user requests them.

How to do it...

  1. The first thing that needs to be done is to let the activity or fragment know that we want to display a popup menu when the user long-taps on a view:
    this.RegisterForContextMenu(someView);
  2. Then, following the pattern of the options menu, we create or inflate the menu items in the OnCreateContextMenu() method:
    public override void OnCreateContextMenu(
      IContextMenu menu,
      View view, 
      IContextMenuContextMenuInfo menuInfo) {
      base.OnCreateContextMenu(menu, view, menuInfo);
      MenuInflater.Inflate(Resource.Menu.Main_Options, menu);
    }
  3. Lastly, we can respond to item selections, as we did with the options menu, in the OnContextItemSelected() method:
    public override bool OnContextItemSelected(IMenuItem item) {
      if (item.ItemId == Resource.Id.action_refresh) {
        // do something here...
        return true;
      }
      return base.OnContextItemSelected(item);
    }

How it works...

We can provide a context menu for any view, but they are most often used for items in a list, grid, or other view collection. One way to show a contextual menu is to use a floating or pop-up menu, and it is the recommended way for apps supporting versions of Android below version 3.0.

Tip

If the views or list view is not registered with its activity or fragment, the context menu will not be displayed, even if the methods are implemented.

When the user long-taps on a view that has been registered for a context menu, the activity or fragment attempts to display a menu that is created or inflated in the OnCreateContextMenu() method.

After the user selects an item from the contextual menu, the OnContextItemSelected() method on the activity or fragment is invoked. In this method, we initiate the desired operation that was selected. We can identify the selected item using the ItemId property.

There's more...

Using the IContextMenu instance that is passed into the OnCreateContextMenu() method, we can change or remove the header of the popup menu. The header could be a combination of an icon and/or text or a separate custom view:

menu.SetHeaderTitle("My Context Menu Heading");

See also

  • The Creating an options menu recipe
  • The Creating contextual action mode menu recipe
You have been reading a chapter from
Xamarin Mobile Development for Android Cookbook
Published in: Nov 2015
Publisher:
ISBN-13: 9781784398576
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image