Android Lollipop, material design changed the
way that apps look and feel to create more magic moments. But devices running
earlier versions of Android don’t have all of
the same options. I’m Joanna Smith, and
one of the coolest things that material design
gave us were tool bars. Which are now even more
magical, because they’re backwards compatible as well. A toolbar is a
view that is richer and more flexible than the
traditional action bar. In fact, toolbars are part
of your view hierarchy, so they can be animated or
even react to scroll events. Because it’s a view,
you can use the toolbar as a standalone element
anywhere in your app, providing the user with
options and controls that don’t need to span
the entire layout width at the very top of the screen. You can even have
multiple toolbars if the structure depends on
the content of your layout, or if you’re providing the user
with many differing controls. To populate your toolbar
with actions and options, you can inflate a
menu onto the toolbar to display those
actions to the user. You can also create
your own controls, like having a navigation
button to go up to the parent activity. While toolbars can be used
in any part of your layout, you can also choose to use the
toolbar instead of the action bar whenever you simply
want more control over the appearance and
functionality of that action bar. So to use the toolbar in place
of the standard action bar, you first need to
disable the action bar. The easiest way to
do this is to have your theme extend from
theme AppCombat.NoActionBar, or its light variant. Then create your
toolbar the way you would create any other
layout in your app, and call set action
bar to designate it as the action bar in the
activity or fragment. By calling this
method, the system will add the standard
action bar options for you, so you don’t need
to inflate a menu. Now you may be convinced of
how awesome toolbars are, but how can you use them
in versions of your app targeting older SDKs? Well, toolbars were
added to the support library for AppCompat V21. So now it’s as easy as calling
set support action bar instead of set action bar to replace
action bars on older devices. So check the documentation
for all of the support options available, so that you can
bring more magic moments to your users. And for more tips on making sure
that your app is at its best, check out the rest of our
Android Development Patterns content, or consider
joining our G+ community. But most importantly, just
continue to build better apps. [MUSIC PLAYING]