Android Programming The Big Nerd Ranch Guide, 3rd Edition

(Brent) #1

Chapter 13  The Toolbar


252

If you have items in the overflow menu, those items will be represented by the three dots on the far-
right side of the toolbar, as shown in Figure 13.4. You will update your code to add additional menu
items in a moment.


Figure 13.4  Overflow menu in the toolbar


Other options for showAsAction include always and never. Using always is not recommended; it is
better to use ifRoom and let the OS decide. Using never is a good choice for less-common actions.
In general, you should only put action items that users will use frequently in the toolbar to avoid
cluttering the screen.


The app namespace


Notice that fragment_crime_list.xml uses the xmlns tag to define a new namespace, app, which is
separate from the usual android namespace declaration. This app namespace is then used to specify
the showAsAction attribute.


This unusual namespace declaration exists for legacy reasons with the AppCompat library. The
action bar APIs were first added in Android 3.0. Originally, the AppCompat library was created to
bundle a compatibility version of the action bar into apps supporting earlier versions of Android, so
that the action bar would exist on any device, even those that did not support the native action bar.
On devices running Android 2.3 or older, menus and their corresponding XML did exist, but the
android:showAsAction attribute was only added with the release of the action bar.


The AppCompat library defines its own custom showAsAction attribute and does not look for the
native showAsAction attribute.

Free download pdf