Hi, I'm Chris Banes, an engineer on the Android
Developer Relations Team.
I'm here today to give you an introduction to
ActionBarCompat.
So, what is ActionBarCompat?
Well, it's a support library that was released this week as
part of the support library version 18 which allows you to
add a compatible Action Bar to your application, that allows
it to work from Android 2.1 upwards.
ActionBarCompat has been designed to mimic the Action
Bar API, available in later versions of the platform, as
much as possible, which makes your job easier.
So how should you actually go about adding it to your
application?
Well, there are two ways we support.
First is via Gradle, and it's been added to the Android
support repository, which means it's a one-line change
to a build.gradle, as you can see here.
Or, alternatively, you could add it as a library project.
You should have a look at the Support Library Release Notes
for more information on both of these.
So, for the rest of this DevByte, I'm
going to go for a scenario.
And that scenario is that you have an application which
works on Ice Cream Sandwich and above, but you want it to
work on devices from 2.1 and above instead.
The first thing we need to look at are your resources,
specifically your menus and your styles.
So ActionBarCompat includes three styles which you must
use either directly or indirectly.
And they are Theme.AppCompat, Theme.AppCompat.Light, and
Theme.AppCompat.
Light.DarkActionBar.
Now the keen-eyed of you will see that each of these mirror
a theme from Theme.Holo and they act accordingly.
So back to our scenario, we are in our manifest, we have
an activity which directly uses the
Theme.Holo.Light.DarkActionBar theme.
And this comes from the Android system.
Now, to change this for ActionBarCompat, we need to
use the theme from ActionBarCompat instead.
So that would be the Theme.AppCompat.
Light.DarkActionBar theme.
But most applications don't use this, and they don't use
themes directly.
They have customized themes.
So here we have a small example with a theme called
Theme.Styled, which has a parent of Theme.Holo.Light
which has a custom action bar theme.
And that style is referenced below.
Now, for ActionBarCompat, we've had to import a lot of
attributes into ActionBarCompat which you have
to then use.
So there are two rules you need to think about.
The first is that your style must always have
an AppCompat parent.
And secondly, you must always double-set each attribute,
once in the Android namespace, and again
in the default namespace.
So back to our scenario.
Theme.Styled will become this.
So rule number one, has to have an AppCompat parent, so
here we've changed it to a Theme.AppCompat.Light.
And then rule number two, you have to
double-set each attribute.
Now, we've already set it in the Android namespace, as you
can see, so we just need to copy that value and do it
again in the default namespace, which is the second
bit in green.
That's it with styles.
As long as you follow those rules, you should be fine.
So, following on with that with menus.
So as with the standard action bar, items are added to it via
the options menu.
And we recommend you add them via menu resources.
So here is an example menu resource, with one item, a
search item.
and that item has an actionViewClass referencing a
SearchView.
Now the trouble with this is that it has two attributes
which were added in Honeycomb, so we can't use them before.
We can, but they'll be ignored.
So to get around this, we've imported all of the action
item related attributes into ActionBarCompat.
So to do that, you need to import
them into your namespace.
Now, we've called it the yourapp namespace, but you
could call it anything you want.
And the result is that for any of the action item attributes,
you need to reference them from this namespace, not the
Android namespace.
So that's it with your resources.
Now we're going to move on to some code.
So the first thing you need to think about is your activity.
Your activities need to extend from ActionBarActivity.
Now ActionBarActivity was added in ActionBarCompat, and
it actually extends from FragmentActivity itself.
That means you can continue using Fragments, but you must
use the support implementation.
Then, as I mentioned earlier, the API we've tried to add to
ActionBarCompat is very similar to that of Activity in
later releases of Android.
So we have a getSupportActionBar method,
which are will return you an action bar, which has a near
identical API to .ActionBar available in Honeycomb ICS.
So you can add tabs, change navigation type, or change the
home item to act as open navigation.
Another method you'll probably use is startSupportActionMode,
which will start a contextual action bar.
Now, as mentioned earlier, you add items to your action bar
via the options menu.
So here is an implementation that you would do in Android
version 1, where you override onCreateOptionsMenu() and you
inflate a menu from your resources.
Now, nothing has changed from this.
It is exactly the same as you do with ActionBarCompat.
The only difference is when you want to start calling
methods, which were added in Honeycomb or later, such as
getActionView we have here, to get the actual reference to
the SearchView.
Now, to get around this in ActionBarCompat, we've added
the MenuItemCompat class, which is a collection of
static methods which will allow you to reference these
methods cleanly and safely, much like you do with a lot of
the other support methods.
Now we move onto fragments.
Now, nothing has changed with fragments.
You can continue to use android.support.v4.Fragment as
you always have.
And, indeed, to add items to your options menu from there,
you just need to call setHasOptionsMenu() during
your onCreate(), and then your onCreateOptionsMenu() and
onPrepareOptionsMenu() will be called.
And you inflate them just like you do in your activity.
Hope this makes your lives easier,
and thanks for watching.