SlideShare a Scribd company logo
AnDevCon SF 2013

The Action Bar,
Front to Back
Copyright © 2012 CommonsWare, LLC
Action Bar Options
●

Native Implementation
●

●

Works well if android:minSdkVersion is 11 or
higher

Backports
●

Official: AppCompat

●

Popular: ActionBarSherlock

Copyright © 2012 CommonsWare, LLC
Action Bar Options
●

AppCompat
●

Part of Android Support package

●

Pros
–
–

●

Google “seal of approval”
No additional libraries (since you probably are already
using the Android Support package)

Cons
–

Released in August 2013

–

Modest track record to date

Copyright © 2012 CommonsWare, LLC
Action Bar Options
●

ActionBarSherlock
●

Independent open source backport

●

Pros
–
–

●

Used by countless projects over 18+ months
Lots of material written about it

Cons
–

Requires an Android library project

–

No Google “seal of approval”

Copyright © 2012 CommonsWare, LLC
Manifest Changes
●

<uses-sdk android:targetSdkVersion=”14”>
●
●

●

Action bar added
Holographic theme for widgets

android:uiOptions=”splitActionBarWhenNarrow”
●

Two halves of action bar, top and bottom

●

Used for phones in portrait mode

Copyright © 2012 CommonsWare, LLC
Populating the Bar
●

res/menu/options.xml (or other name)
●

Root = <menu>

●

<menu> holds <item> elements

●

<item> defines a single menu item
–

android:id

–

android:title

–

android:icon

–

android:visible

–

android:enabled

Copyright © 2012 CommonsWare, LLC
Populating the Bar
●

Activity Callbacks for Action Bar
●

onCreateOptionsMenu()
–

●

onOptionsItemSelected()
–

●

Where you set up the basic options menu to use, via a
MenuInflater
Called when the user clicks on something in the action
bar

Default Behavior: Overflow
●

Devices with MENU button – use to display

●

Devices sans MENU button – … to display

Copyright © 2012 CommonsWare, LLC
Toolbar Buttons
●

●

Puts item right in action bar, vs. overflow
Add android:showAsAction to <item> in
menu resource
●

●

ifRoom to indicate it can remain an options menu
item if there is no room
withText if you want icon & title

Copyright © 2012 CommonsWare, LLC
Fragments and the Action Bar
●

Step #1: Call setHasOptionsMenu(true)

●

Step #2: Implement onCreateOptionsMenu()
●

●

Slightly different method signature

Step #3: Implement onOptionsItemSelected()

Copyright © 2012 CommonsWare, LLC
Custom Action Bar Widgets
●

Option #1: Substitute Own Inflated Layout for
Standard Button
●

●

Add android:actionLayout to <item> in menu
resource
Call getActionView() on MenuItem to
configure at runtime

Copyright © 2012 CommonsWare, LLC
Custom Action Bar Widgets
●

Option #2: android:actionViewClass
●

Skip the layout, directly reference a View class

●

Often implements CollapsibleActionView interface
–

●

Allows automatic expansion to fill available space or
collapse to allow other action bar items to be seen

Built-In: SearchView

Copyright © 2012 CommonsWare, LLC
Custom Action Bar Widgets
●

Option #3: ActionProvider
●

●

Extend ActionProvider, implement
onCreateActionView()
Wire in via android:actionProviderClass in
menu resource

●

Supports overflow with simplified UI

●

Built-in
–

ShareActionProvider

–

MediaRouteActionProvider

Copyright © 2012 CommonsWare, LLC
ShareActionProvider
●

Stock Way to Share Content

●

Step #1: Add to <menu>

●

Step #2: Call setShareIntent()
●

●

●

Once or many times, as appropriate
Be sure to set MIME type!

Optional
●

Control share history

●

Register OnShareTargetSelectedListener, to update UI

Copyright © 2012 CommonsWare, LLC
SearchView
●

The Classic Magnifying Glass

●

Approaches
●

Iconified by default, expanding on click

●

Expanded by default
–

Good for tablets, particularly in landscape

Copyright © 2012 CommonsWare, LLC
Basic SearchView Usage
●

Step #1: Add to <menu>

●

Step #2: Configure in onCreateOptionsMenu()
●

Register listeners
–
–

●

●

OnQueryTextListener
OnCloseListener

Other settings

Step #3: Respond to Events
●

E.g., manage a ListView filter

Copyright © 2012 CommonsWare, LLC
App Icon
●

Default = activity icon
●

●

Virtual menu choice: android.R.id.home
●

●

Optional android:logo in <application> to
supply alternative image
Handle in onOptionsItemSelected()

setDisplayHomeAsUpEnabled()
●
●

Adds arrowhead
Handling “up navigation” well is beyond the
scope of this presentation

Copyright © 2012 CommonsWare, LLC
Action Bar Navigation
●

Option #1: Tabs
●

Use setNavigationMode() on ActionBar
–

NAVIGATION_MODE_TABS

●

Call addTab() to add a tab

●

Pros: easy to set up, automatic fragment support

●

Cons
–

May appear on separate row

–

May be converted into list navigation

Copyright © 2012 CommonsWare, LLC
Action Bar Navigation
●

Option #2: List
●

Use setNavigationMode() on ActionBar
–

●

NAVIGATION_MODE_LIST

Call setListNavigationCallbacks() to define
Spinner contents and listener

Copyright © 2012 CommonsWare, LLC
Action Bar Navigation
●

setCustomView()
●
●

●
●

You supply your own View or layout resource ID
Used in the navigation space on the action bar,
instead of tabs or Spinner
Example: AutoCompleteTextView for browser
getCustomView() to retrieve inflated layout for
runtime configuration

Copyright © 2012 CommonsWare, LLC
Action Modes
●

Alternate Action Bar for Contextual Actions
●

Operations on selections
–
–

●

Multiple selections in a list
Selected text in a TextView, EditText, WebView, etc.

Replacement for context menu

Copyright © 2012 CommonsWare, LLC
Action Modes
●

ActionMode.Callback
●

●

Configure ActionMode in
onCreateActionMode()
onActionItemClicked() if user clicks a toolbar
button

●

finish() the ActionMode when done

●

Clean up in onDestroyActionMode()

Copyright © 2012 CommonsWare, LLC
Action Modes
●

Automatic Multiple-Choice Action Mode
●

CHOICE_MODE_MULTIPLE_MODAL and an

appropriate row layout
●

Checking item toggles on action mode with your
supplied MultiChoiceModeListener callback
–

Serves as ActionBar.Callback, plus
onItemCheckedStateChanged() for check/uncheck
events

Copyright © 2012 CommonsWare, LLC
Action Modes
●

Long-Press-Initiated Automatic Action Mode
●

Start off in single-choice mode

●

On long-click of item, toggle into
CHOICE_MODE_MULTIPLE_MODAL

●

●

When action mode destroyed, switch back to
single-choice mode
Need to remember choice mode across
configuration changes!

Copyright © 2012 CommonsWare, LLC
Styles and Themes
●

Theme.Holo / Theme.Holo.Light
●

●

Standard themes, standard color scheme

Can Style the Action Bar
●

Easy: Action Bar Style Generator
–

●

http://jgilfelt.github.io/android-actionbarstylegenerator

More power: DIY
–

https://developer.android.com/training/basics/actionbar
/styling.html

Copyright © 2012 CommonsWare, LLC
What Else Is There?
●

Custom Action Providers

●

ActionBarDrawerToggle

●

Transparent/Translucent Action Bars

●

Checkable Action Items

●

Long-Press “Tooltip” Help

●

And more!

Copyright © 2012 CommonsWare, LLC

More Related Content

PDF
Mastering the Master Detail Pattern
PPTX
What's new in android jakarta gdg (2015-08-26)
PDF
Mooscon 2013 cebit - google integration in android apps (1)
PDF
Android RuntimePermissionsExtended
PDF
Android Development...Using Web Technologies
PDF
The Action Bar: Front to Back
ODP
Android App Development - 05 Action bar
PPTX
Chapter 2 lesson-1 adding the action bar
Mastering the Master Detail Pattern
What's new in android jakarta gdg (2015-08-26)
Mooscon 2013 cebit - google integration in android apps (1)
Android RuntimePermissionsExtended
Android Development...Using Web Technologies
The Action Bar: Front to Back
Android App Development - 05 Action bar
Chapter 2 lesson-1 adding the action bar

Similar to The Action Bar: Front to Back (20)

PDF
Action Bar in Android
PDF
Using android's action bar
PDF
Android Support Library: Using ActionBarCompat
PPTX
ActionBarCompat Tutorial-Part 1(Prepare and Setup)
PDF
Chapt 04 user interaction
PPTX
Action bar & ActionBarSherlock
PDF
Lecture-10-Menus.pdf of Mobile Application Development
PPTX
Action Bar Sherlock tutorial
PPTX
Android ui with xml
PDF
Android ActionBar Navigation reloaded
PPTX
Handling action bar in Android
DOCX
Android action bar and notifications-chapter16
DOCX
Android menus in android-chapter15
PDF
Android Training - Action Bar
PDF
Android design patterns
PDF
Action bar
PPTX
Chapter 2 lesson-2 styling the action bar
PDF
2012/02/15 Android 4.0 UI Design Tips@happy designer meetup
PDF
01 10 - graphical user interface - others
PDF
Android 4.0 UI Design Tips
Action Bar in Android
Using android's action bar
Android Support Library: Using ActionBarCompat
ActionBarCompat Tutorial-Part 1(Prepare and Setup)
Chapt 04 user interaction
Action bar & ActionBarSherlock
Lecture-10-Menus.pdf of Mobile Application Development
Action Bar Sherlock tutorial
Android ui with xml
Android ActionBar Navigation reloaded
Handling action bar in Android
Android action bar and notifications-chapter16
Android menus in android-chapter15
Android Training - Action Bar
Android design patterns
Action bar
Chapter 2 lesson-2 styling the action bar
2012/02/15 Android 4.0 UI Design Tips@happy designer meetup
01 10 - graphical user interface - others
Android 4.0 UI Design Tips
Ad

More from CommonsWare (20)

PDF
Gradle and Your Android Wearable Projects
PDF
Getting Android Developers for Your Wearables
PDF
When Microwatts Are Precious: Battery Tips for Wearable Apps
PDF
Android Security: Defending Your Users
PDF
Secondary Screen Support Using DisplayManager
PDF
Not Quite As Painful Threading
PDF
Android Development: The 20,000-Foot View
PDF
Maps V2... And You!
PDF
A Deep Dive Into ViewPager
PDF
Second-Screen Support in Android 4.2
PDF
Integrate Android Apps and Web Apps
PDF
From Android to the Mobile Web
PDF
X Means Y
PDF
The Wonderful World of Wearables
PDF
Securing User Data with SQLCipher
PDF
Beaming Data to Devices with NFC
PDF
What's New in Jelly Bean
PDF
Making Money at Mobile: 60 Business Models
PDF
AppsWorld Keynote
PDF
App Integration (Revised and Updated)
Gradle and Your Android Wearable Projects
Getting Android Developers for Your Wearables
When Microwatts Are Precious: Battery Tips for Wearable Apps
Android Security: Defending Your Users
Secondary Screen Support Using DisplayManager
Not Quite As Painful Threading
Android Development: The 20,000-Foot View
Maps V2... And You!
A Deep Dive Into ViewPager
Second-Screen Support in Android 4.2
Integrate Android Apps and Web Apps
From Android to the Mobile Web
X Means Y
The Wonderful World of Wearables
Securing User Data with SQLCipher
Beaming Data to Devices with NFC
What's New in Jelly Bean
Making Money at Mobile: 60 Business Models
AppsWorld Keynote
App Integration (Revised and Updated)
Ad

Recently uploaded (20)

PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Empathic Computing: Creating Shared Understanding
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
KodekX | Application Modernization Development
PDF
Encapsulation theory and applications.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Machine learning based COVID-19 study performance prediction
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Approach and Philosophy of On baking technology
“AI and Expert System Decision Support & Business Intelligence Systems”
Review of recent advances in non-invasive hemoglobin estimation
20250228 LYD VKU AI Blended-Learning.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Empathic Computing: Creating Shared Understanding
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
KodekX | Application Modernization Development
Encapsulation theory and applications.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Machine learning based COVID-19 study performance prediction
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Reach Out and Touch Someone: Haptics and Empathic Computing
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Mobile App Security Testing_ A Comprehensive Guide.pdf
NewMind AI Weekly Chronicles - August'25 Week I
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
MYSQL Presentation for SQL database connectivity
Dropbox Q2 2025 Financial Results & Investor Presentation
Approach and Philosophy of On baking technology

The Action Bar: Front to Back

  • 1. AnDevCon SF 2013 The Action Bar, Front to Back Copyright © 2012 CommonsWare, LLC
  • 2. Action Bar Options ● Native Implementation ● ● Works well if android:minSdkVersion is 11 or higher Backports ● Official: AppCompat ● Popular: ActionBarSherlock Copyright © 2012 CommonsWare, LLC
  • 3. Action Bar Options ● AppCompat ● Part of Android Support package ● Pros – – ● Google “seal of approval” No additional libraries (since you probably are already using the Android Support package) Cons – Released in August 2013 – Modest track record to date Copyright © 2012 CommonsWare, LLC
  • 4. Action Bar Options ● ActionBarSherlock ● Independent open source backport ● Pros – – ● Used by countless projects over 18+ months Lots of material written about it Cons – Requires an Android library project – No Google “seal of approval” Copyright © 2012 CommonsWare, LLC
  • 5. Manifest Changes ● <uses-sdk android:targetSdkVersion=”14”> ● ● ● Action bar added Holographic theme for widgets android:uiOptions=”splitActionBarWhenNarrow” ● Two halves of action bar, top and bottom ● Used for phones in portrait mode Copyright © 2012 CommonsWare, LLC
  • 6. Populating the Bar ● res/menu/options.xml (or other name) ● Root = <menu> ● <menu> holds <item> elements ● <item> defines a single menu item – android:id – android:title – android:icon – android:visible – android:enabled Copyright © 2012 CommonsWare, LLC
  • 7. Populating the Bar ● Activity Callbacks for Action Bar ● onCreateOptionsMenu() – ● onOptionsItemSelected() – ● Where you set up the basic options menu to use, via a MenuInflater Called when the user clicks on something in the action bar Default Behavior: Overflow ● Devices with MENU button – use to display ● Devices sans MENU button – … to display Copyright © 2012 CommonsWare, LLC
  • 8. Toolbar Buttons ● ● Puts item right in action bar, vs. overflow Add android:showAsAction to <item> in menu resource ● ● ifRoom to indicate it can remain an options menu item if there is no room withText if you want icon & title Copyright © 2012 CommonsWare, LLC
  • 9. Fragments and the Action Bar ● Step #1: Call setHasOptionsMenu(true) ● Step #2: Implement onCreateOptionsMenu() ● ● Slightly different method signature Step #3: Implement onOptionsItemSelected() Copyright © 2012 CommonsWare, LLC
  • 10. Custom Action Bar Widgets ● Option #1: Substitute Own Inflated Layout for Standard Button ● ● Add android:actionLayout to <item> in menu resource Call getActionView() on MenuItem to configure at runtime Copyright © 2012 CommonsWare, LLC
  • 11. Custom Action Bar Widgets ● Option #2: android:actionViewClass ● Skip the layout, directly reference a View class ● Often implements CollapsibleActionView interface – ● Allows automatic expansion to fill available space or collapse to allow other action bar items to be seen Built-In: SearchView Copyright © 2012 CommonsWare, LLC
  • 12. Custom Action Bar Widgets ● Option #3: ActionProvider ● ● Extend ActionProvider, implement onCreateActionView() Wire in via android:actionProviderClass in menu resource ● Supports overflow with simplified UI ● Built-in – ShareActionProvider – MediaRouteActionProvider Copyright © 2012 CommonsWare, LLC
  • 13. ShareActionProvider ● Stock Way to Share Content ● Step #1: Add to <menu> ● Step #2: Call setShareIntent() ● ● ● Once or many times, as appropriate Be sure to set MIME type! Optional ● Control share history ● Register OnShareTargetSelectedListener, to update UI Copyright © 2012 CommonsWare, LLC
  • 14. SearchView ● The Classic Magnifying Glass ● Approaches ● Iconified by default, expanding on click ● Expanded by default – Good for tablets, particularly in landscape Copyright © 2012 CommonsWare, LLC
  • 15. Basic SearchView Usage ● Step #1: Add to <menu> ● Step #2: Configure in onCreateOptionsMenu() ● Register listeners – – ● ● OnQueryTextListener OnCloseListener Other settings Step #3: Respond to Events ● E.g., manage a ListView filter Copyright © 2012 CommonsWare, LLC
  • 16. App Icon ● Default = activity icon ● ● Virtual menu choice: android.R.id.home ● ● Optional android:logo in <application> to supply alternative image Handle in onOptionsItemSelected() setDisplayHomeAsUpEnabled() ● ● Adds arrowhead Handling “up navigation” well is beyond the scope of this presentation Copyright © 2012 CommonsWare, LLC
  • 17. Action Bar Navigation ● Option #1: Tabs ● Use setNavigationMode() on ActionBar – NAVIGATION_MODE_TABS ● Call addTab() to add a tab ● Pros: easy to set up, automatic fragment support ● Cons – May appear on separate row – May be converted into list navigation Copyright © 2012 CommonsWare, LLC
  • 18. Action Bar Navigation ● Option #2: List ● Use setNavigationMode() on ActionBar – ● NAVIGATION_MODE_LIST Call setListNavigationCallbacks() to define Spinner contents and listener Copyright © 2012 CommonsWare, LLC
  • 19. Action Bar Navigation ● setCustomView() ● ● ● ● You supply your own View or layout resource ID Used in the navigation space on the action bar, instead of tabs or Spinner Example: AutoCompleteTextView for browser getCustomView() to retrieve inflated layout for runtime configuration Copyright © 2012 CommonsWare, LLC
  • 20. Action Modes ● Alternate Action Bar for Contextual Actions ● Operations on selections – – ● Multiple selections in a list Selected text in a TextView, EditText, WebView, etc. Replacement for context menu Copyright © 2012 CommonsWare, LLC
  • 21. Action Modes ● ActionMode.Callback ● ● Configure ActionMode in onCreateActionMode() onActionItemClicked() if user clicks a toolbar button ● finish() the ActionMode when done ● Clean up in onDestroyActionMode() Copyright © 2012 CommonsWare, LLC
  • 22. Action Modes ● Automatic Multiple-Choice Action Mode ● CHOICE_MODE_MULTIPLE_MODAL and an appropriate row layout ● Checking item toggles on action mode with your supplied MultiChoiceModeListener callback – Serves as ActionBar.Callback, plus onItemCheckedStateChanged() for check/uncheck events Copyright © 2012 CommonsWare, LLC
  • 23. Action Modes ● Long-Press-Initiated Automatic Action Mode ● Start off in single-choice mode ● On long-click of item, toggle into CHOICE_MODE_MULTIPLE_MODAL ● ● When action mode destroyed, switch back to single-choice mode Need to remember choice mode across configuration changes! Copyright © 2012 CommonsWare, LLC
  • 24. Styles and Themes ● Theme.Holo / Theme.Holo.Light ● ● Standard themes, standard color scheme Can Style the Action Bar ● Easy: Action Bar Style Generator – ● http://jgilfelt.github.io/android-actionbarstylegenerator More power: DIY – https://developer.android.com/training/basics/actionbar /styling.html Copyright © 2012 CommonsWare, LLC
  • 25. What Else Is There? ● Custom Action Providers ● ActionBarDrawerToggle ● Transparent/Translucent Action Bars ● Checkable Action Items ● Long-Press “Tooltip” Help ● And more! Copyright © 2012 CommonsWare, LLC