Slide 1

Slide 1 text

ActionBarCompat Chris Banes, Android Developer Programs Engineer

Slide 2

Slide 2 text

ActionBarCompat What is it? A support library which provides a compatible Action Bar to your application, targeting Android v2.1+ Designed to mirror the Action Bar API found in later platforms, as closely as possible

Slide 3

Slide 3 text

ActionBarCompat Add to project Add dependency to build.gradle: Alternatively, add it as a library project See the Support Library Release Notes for more info dependencies { compile "com.android.support:appcompat-v7:18.0.+" ... }

Slide 4

Slide 4 text

Resources

Slide 5

Slide 5 text

ActionBarCompat Styles You need to use one of the following themes: @style/Theme.AppCompat @style/Theme.AppCompat.Light @style/Theme.AppCompat.Light.DarkActionBar

Slide 6

Slide 6 text

ActionBarCompat Styles If you have the following set in the AndroidManifest.xml: ...becomes:

Slide 7

Slide 7 text

ActionBarCompat Styles - Customized More tricky... <item name="android:actionBarStyle"> @style/Widget.Styled.ActionBar </item> <item name="android:background">@drawable/ab_custom_solid_styled</item> <item name="android:backgroundStacked"> @drawable/ab_custom_stacked_solid_styled </item> <item name="android:backgroundSplit"> @drawable/ab_custom_bottom_solid_styled </item>

Slide 8

Slide 8 text

ActionBarCompat Styles - Customized Converting styles rules Use an AppCompat style as the parent Double-set each attribute, once in the android namespace and again the default namespace

Slide 9

Slide 9 text

ActionBarCompat Styles - Customized Becomes: res/values-v14 res/values 
 <!-- Setting values in the android namespace affects API v14+ -->
 <item name="android:actionBarStyle"> @style/Widget.Styled.ActionBar </item>
 <!-- Setting values in the default namespace affects API v7-13 -->
 <item name="actionBarStyle"> @style/Widget.Styled.ActionBar </item>


Slide 10

Slide 10 text

ActionBarCompat Menu As with the standard Action Bar, items are added via the menu:

Slide 11

Slide 11 text

ActionBarCompat Menu As with the standard Action Bar, items are added via the menu:

Slide 12

Slide 12 text

Code

Slide 13

Slide 13 text

ActionBarCompat Activity Activities need to extend from: Provides an API which is very similar to that found in Activity: android.support.v7.app.ActionBarActivity getSupportActionBar() startSupportActionMode()

Slide 14

Slide 14 text

ActionBarCompat Activity How to add items to the Action Bar: @Override
 public boolean onCreateOptionsMenu(Menu menu) {
 // Inflate the menu
 getMenuInflater().inflate(R.menu.main_menu, menu);
 
 // Find the search item
 MenuItem searchItem = menu.findItem(R.id.menu_search);
 
 // Retrieve the SearchView
 mSearchView = (SearchView) searchItem.getActionView();
 
 return super.onCreateOptionsMenu(menu);
 }

Slide 15

Slide 15 text

ActionBarCompat Activity How to add items to the Action Bar: @Override
 public boolean onCreateOptionsMenu(Menu menu) {
 // Inflate the menu
 getMenuInflater().inflate(R.menu.main_menu, menu);
 
 // Find the search item
 MenuItem searchItem = menu.findItem(R.id.menu_search);
 
 // Retrieve the SearchView
 mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
 
 return super.onCreateOptionsMenu(menu);
 }

Slide 16

Slide 16 text

ActionBarCompat Fragments Use android.support.v4.Fragment as your base class Call setHasOptionsMenu() during onCreate() Override onCreateOptionsMenu() and onPrepareOptionsMenu()

Slide 17

Slide 17 text

ActionBarCompat Split Action Bar Add meta-data element to each Activity element in AndroidManifest.xml ... API v14+ API v7+

Slide 18

Slide 18 text

Extras

Slide 19

Slide 19 text

ActionBarCompat SearchView Backport available: android.support.v7.widget.SearchView Be careful with: setSearchableInfo(SearchableInfo) SearchableInfo was added in API v8

Slide 20

Slide 20 text

ActionBarCompat ShareActionProvider Backport available: android.support.v7.widget.ShareActionProvider

Slide 21

Slide 21 text

ActionBarCompat ShareActionProvider Backport available: android.support.v7.widget.ShareActionProvider @Override
 public boolean onCreateOptionsMenu(Menu menu) {
 // Inflate the menu
 getMenuInflater().inflate(R.menu.main_menu, menu);
 
 // Find the share item
 MenuItem shareItem = menu.findItem(R.id.menu_share);
 
 // Retrieve the SearchView
 mShareAp = (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem);
 
 return super.onCreateOptionsMenu(menu);
 } // Later when you have created a share intent mShareAp.setShareIntent(shareIntentForContent);

Slide 22

Slide 22 text

ActionBarCompat PopupMenu Backport available: android.support.v7.widget.PopupMenu // Create a PopupMenu, giving it the clicked view for an anchor PopupMenu popup = new PopupMenu(getActivity(), view); // Inflate our menu resource into the PopupMenu's Menu popup.getMenuInflater().inflate(R.menu.popup, popup.getMenu()); // Set a listener so we are notified if a menu item is clicked popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem menuItem) { ... } }); // Finally show the PopupMenu popup.show();

Slide 23

Slide 23 text

ActionBarCompat ActionBarDrawerToggle Support Automatic support for ActionBarDrawerToggle Need to be using ActionBarActivity // Enable Home button and set to display as up navigation getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true); // Create ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close); // Finally set the ActionBarDrawerToggle as the DrawerListener mDrawerLayout.setDrawerListener(mDrawerToggle);

Slide 24

Slide 24 text

ActionBarCompat Progress Bars @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Needs to be called before setting the content view supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); // Now set the content view setContentView(R.layout.activity_main); } // When ready, show the indeterminate progress bar setSupportProgressBarIndeterminateVisibility(true);

Slide 25

Slide 25 text

ActionBarCompat Ancestral Navigation Also known as ‘Up Navigation’
 In your AndroidManifest.xml: In your Activity: // Display home item as up navigation getSupportActionBar().setDisplayHomeAsUpEnabled(true); API v16+ API v7+

Slide 26

Slide 26 text

ActionBarCompat Ancestral Navigation onSupportNavigateUp() getSupportParentActivityIntent() supportShouldUpRecreateTask() onPrepareSupportNavigateUpTaskStack() User clicks Home Parent Activity Started onCreateSupportNavigateUpTaskStack() true supportNavigateUpTo() false Looks at AndroidManifest parent activity Checks if we’re crossing tasks

Slide 27

Slide 27 text

Action Bar Style Generator Now supports ActionBarCompat! http://www.actionbarstylegenerator.com

Slide 28

Slide 28 text

ActionBar-PullToRefresh Now supports ActionBarCompat too! https://github.com/chrisbanes/ActionBar-PullToRefresh ...yes I know it’s a shameless plug

Slide 29

Slide 29 text

ActionBarCompat Find Bugs? http://b.android.com

Slide 30

Slide 30 text

Any questions?