Android Programming The Big Nerd Ranch Guide by Bill Phillips, Chris Stewart, Kristin Marsicano (z-lib.org)

(gtxtreme123) #1

For the More Curious: Action Bar vs Toolbar


For the More Curious: Action Bar vs Toolbar


Figure 13.13  Action Bar vs Toolbar


What is the difference between the action bar and the toolbar?


The most obvious difference is the updated visual design of the toolbar. The toolbar no longer includes
an icon on the left side and decreases some of the spacing between the action items on the right side.
Another major visual change is the Up button. In the action bar, this button was much more subtle and
was just an accessory next to the icon in the action bar.


Aside from the visual differences, the main goal of the toolbar is to be more flexible than the action
bar. The action bar has many constraints. It will always appear at the top of the screen. There can only
be one action bar. The size of the action bar is fixed and should not be changed. The toolbar does not
have these constraints.


In this chapter, you used a toolbar that was provided by one of the AppCompat themes. Alternatively,
you can manually include a toolbar as a normal view in your activity or fragment’s layout file. You can
place this toolbar anywhere you like and you can even include multiple toolbars on the screen at the
same time. This flexibility allows for interesting designs; for example, imagine if each fragment that
you use maintains its own toolbar. When you host multiple fragments on the screen at the same time,
each of them can bring along its own toolbar instead of sharing a single toolbar at the top of the screen.


Another interesting addition with the toolbar is the ability to place Views inside of the toolbar and to
also adjust the height of the toolbar. This allows for much more flexibility in the way that your app
works.


Challenge: Deleting Crimes


Once a crime has been created in CriminalIntent, there is no way to erase that crime from the official
record. For this challenge, add a new action item to the CrimeFragment that allows the user to delete
the current crime. Once the user presses the new delete action item, be sure to pop the user back to the
previous activity with a call to the finish() method on the CrimeFragment’s hosting activity.

Free download pdf