Pages

Thursday, November 6, 2014

Toolbar Widget

Toolbar
is fully supported in AppCompat and has feature and API parity with
the framework widget. In AppCompat,Toolbar
is implemented in the android.support.v7.widget.Toolbar
class. There are two ways to use Toolbar:

Use
a Toolbar
as an Action Bar when you want to use the existing Action Bar
facilities (such as menu inflation and selection,
ActionBarDrawerToggle,
and so on) but want to have more control over its appearance.

Use
a standalone Toolbar
when you want to use the pattern in your app for situations that an
Action Bar would not support; for example, showing multiple toolbars
on the screen, spanning only part of the width, and so on.

Toolbar
supports a more focused feature set than ActionBar. From
start to end, a toolbar may contain a combination of the following
optional elements:

A
navigation button

A
branded logo image

A
title and subtitle

One
or more custom views

An
action menu

Things
to do remind while Implementing ToolBar

IllegalStateException
(windowActionBar)

java.lang.RuntimeException:
Unable to start activity ComponentInfo{}:
java.lang.IllegalStateException: This Activity already has an action
bar supplied by the window decor. Do not request
Window.FEATURE_ACTION_BAR and set windowActionBar to false in your
theme to use a Toolbar instead.

ThemeOverlay.AppCompat.Light
: Changes the Toolbar title text
color to Black.

ThemeOverlay.AppCompat.Dark.ActionBar
: Changes the Toolbar title text
color to White.

Usage
of inflateMenu in Toolbar:

If
you're using setSupportActionBar(toolbar);theninflateMenu
of toolbar cannot be used, because Toolbar is acting like Actionbar.
So, all the menu related callbacks are default.

The
only time you need to call toolbar.inflateMenu()is
when you are using the Toolbar as a standalone widget. In this case
you need to manually populate the Toolbar with content/actions. For
instance, if you want it to display actions, you need to inflate a
menu into it:

You can download the source code by clicking here: ToolBarMaterialComp Source Code. This project is built using eclipse IDE. Unzip and import the project into Eclipse, it’s a good idea to use the Project by clean and rebuild from the project menu. It works in all API levels above 2.3.

As always, thanks for reading :)

Don't forget to +1 this blog and share this post on Google+ if you found it interesting!Have something to add to this post? If you have any other quick thoughts/hints that you think people will find useful? Share it in the comments & feedback's are welcome.