Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

ActionBarCompat (Londroid)

ActionBarCompat (Londroid)

Chris Banes

July 31, 2013
Tweet

More Decks by Chris Banes

Other Decks in Technology

Transcript

  1. 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
  2. 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.+" ... }
  3. ActionBarCompat Styles You need to use one of the following

    themes: @style/Theme.AppCompat @style/Theme.AppCompat.Light @style/Theme.AppCompat.Light.DarkActionBar
  4. ActionBarCompat Styles If you have the following set in the

    AndroidManifest.xml: ...becomes: <activity ... android:theme="@android:style/Theme.Holo.Light.DarkActionBar" /> <activity ... android:theme="@style/Theme.AppCompat.Light.DarkActionBar" />
  5. ActionBarCompat Styles - Customized More tricky... <style name="Theme.Styled" parent="@android:style/Theme.Holo.Light"> <item

    name="android:actionBarStyle"> @style/Widget.Styled.ActionBar </item> </style> <style name="Widget.Styled.ActionBar" parent="@android:style/Widget.Holo.Light.ActionBar"> <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> </style>
  6. 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
  7. ActionBarCompat Styles - Customized Becomes: res/values-v14 res/values <style name="Theme.Styled" parent="@style/Theme.AppCompat.Light">


    <!-- Setting values in the android namespace affects API v14+ -->
 <item name="android:actionBarStyle"> @style/Widget.Styled.ActionBar </item>
 </style> <style name="Theme.Styled" parent="@style/Theme.AppCompat.Light"> <!-- Setting values in the default namespace affects API v7-13 -->
 <item name="actionBarStyle"> @style/Widget.Styled.ActionBar </item>
 </style>
  8. ActionBarCompat Menu As with the standard Action Bar, items are

    added via the menu: <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menu_search" android:title="@string/menu_search" android:actionViewClass="android.widget.SearchView" android:showAsAction="always" /> <!-- Your other items... --> </menu>
  9. ActionBarCompat Menu As with the standard Action Bar, items are

    added via the menu: <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:yourapp="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/menu_search" android:title="@string/menu_search" yourapp:actionViewClass="android.support.v7.widget.SearchView" yourapp:showAsAction="always" /> <!-- Your other items... --> </menu>
  10. 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()
  11. 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);
 }
  12. 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);
 }
  13. ActionBarCompat Fragments Use android.support.v4.Fragment as your base class Call setHasOptionsMenu()

    during onCreate() Override onCreateOptionsMenu() and onPrepareOptionsMenu()
  14. ActionBarCompat Split Action Bar Add meta-data element to each Activity

    element in AndroidManifest.xml <activity android:name=".YourActivity" android:uiOptions="splitActionBarWhenNarrow"> <intent-filter> ... </intent-filter> <meta-data android:name="android.support.UI_OPTIONS" android:value="splitActionBarWhenNarrow" /> </activity> API v14+ API v7+
  15. 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);
  16. 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();
  17. 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);
  18. 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);
  19. ActionBarCompat Ancestral Navigation Also known as ‘Up Navigation’
 In your

    AndroidManifest.xml: In your Activity: // Display home item as up navigation getSupportActionBar().setDisplayHomeAsUpEnabled(true); <activity android:name=".YourActivity" android:parentActivityName=".YourParentActivity"> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".YourParentActivity" /> </activity> API v16+ API v7+
  20. 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