The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.


Toolbar

Android 5.0 introduced a Toolbar widget, offering functionality akin to the action bar, but in the form of a ViewGroup that can be positioned where you need it. You can even use a Toolbar as an outright replacement for the action bar, for cases where you need a bit more control over the action bar implementation than you get by default.

In this chapter, we will explore the use of Toolbar. Note that an upcoming chapter will cover the use of a backport of Toolbar that works back to API Level 7… albeit with some issues.

Prerequisites

Understanding this chapter requires that you have read the core chapters, particularly the one on the action bar.

Note that the examples in this chapter are clones of a couple from the core chapters. This chapter’s prose was written assuming that you were familiar with those samples, so you may need to go back and review them as needed.

One of the samples relies upon using a custom Parcelable class, which is covered in another chapter.

Basic Toolbar Mechanics

As noted earlier, a Toolbar is an ordinary ViewGroup. While it does not support placing arbitrary children in it the way a LinearLayout might, it otherwise can be used like any other ViewGroup. In particular, you can put it in a layout resource and position it wherever it makes sense, such as in a lower quadrant of a tablet-sized screen, tied to some specific part of your UI.

However, the Toolbar is not the action bar… at least, not by default. As such, you will use somewhat different methods for interacting with it, particularly for dealing with menu items:

A Toolbar does not automatically adopt much in the way of styling from your activity’s theme. In particular, it does not set the background color to be the primary color of a Theme.Material theme, the way the action bar does. However, whether via a style resource, XML attributes in a layout file, or Java code, you can affect these same sorts of capabilities.

Use Case #1: Split Action Bar

The preview of this section is presently indisposed.

Use Case #2: Contextual Actions

The preview of this section is en route to Mars.

Use Case #3: Replacement Action Bar

The preview of this section was traded for a bag of magic beans.