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

Adding action bar action items

The fundamental purpose of an action bar, besides navigation, is to present the user with a set of actions that can be performed.

How to do it...

By simply using the action bar, all the action items are added to the overflow:

  1. The XML for ActionBar items is exactly the same as the options menu:
    <menu ... >
      <item
        android:id="@+id/action_refresh"
        android:icon="@drawable/ic_action_refresh"
        android:title="@string/action_refresh"/>
    </menu>

However, we can customize what items are displayed, and how they are displayed:

  1. To add action items with images to the actual ActionBar property, as well as more complex items, all that is needed is an attribute in the XML, showAsAction:
    <menu ... xmlns:app="http://schemas.android.com/apk/res-auto">
      <item ... app:showAsAction="ifRoom"/>
    </menu>
  2. If we wish to add custom views, such as a search box, to the action bar, we make use of the actionViewClass attribute:
    <menu ... xmlns:app="http://schemas.android.com/apk/res-auto">
      <item ...
      app:actionViewClass="android.support.v7.widget.SearchView"/>
    </menu>
  3. If the view is in a layout resource file, we use the actionLayout attribute:
    <menu ... xmlns:app="http://schemas.android.com/apk/res-auto">
      <item ... app:actionLayout="@layout/action_rating"/>
    </menu>
  4. Sometimes, we may wish to only display the icon initially and then, when the user taps the icon, expand the item to display the action view:
    <menu ... xmlns:app="http://schemas.android.com/apk/res-auto">
      <item ... app:showAsAction="ifRoom|collapseActionView"/>
    </menu>

How it works...

Action item buttons are just traditional options menu items but are optionally always visible on the action bar.

The underlying logic to handle item selections is the same as that for the traditional options menu. No change is required to existing code inside the OnOptionsItemSelected() method.

The value of the showAsAction attribute can be ifRoom, never, or always. This value can optionally be combined, using a pipe, with withText and/or collapseActionView.

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