A Toolbar is a generalization of action bars for use within application layouts.
Traditionally, an actionbar has always part of an Activity's opaque window decor controlled by the framework.
Toolbar on the other hand may be placed at any arbitrary level of nesting within a view hierarchy.
ToolBar API Definition
ToolBar derives from ViewGroup and was added in
Here's the API definition of
Which Features can be Displayed in a ToolBar?
Toolbar supports a more focused feature set than ActionBar.
You can display the following in a ToolBar:
- NavigationButton : This may be an Up arrow, navigation menu toggle, close, collapse, done or another glyph of the app's choosing.This button should always be used to access other navigational destinations within the container of the Toolbar and its signified content or otherwise leave the current context signified by the Toolbar.The navigation button is vertically aligned within the Toolbar's minimum height, if set.
- A branded logo image : This may extend to the height of the bar and can be arbitrarily wide.
- A title and subtitle : The title should be a signpost for the Toolbar's current position in the navigation hierarchy and the content contained there. The subtitle, if present should indicate any extended information about the current content. If an app uses a logo image it should strongly consider omitting a title and subtitle.
- One or more custom views : The application may add arbitrary child views to the Toolbar. They will appear at this position within the layout. If a child view's
CENTER_HORIZONTALthe view will attempt to center within the available space remaining in the Toolbar after all other elements have been measured.
- An action menu : The menu of actions will pin to the end of the Toolbar offering a few frequent, important or typical actions along with an optional overflow menu for additional actions. Action buttons are vertically aligned within the Toolbar's minimum height, if set.
How can we use ToolBar as an ActionBar?
All you need to use a ToolBar as an action bar is the
setSupportActionBar() method defined in AppCompatActivity class.
setSupportActionBar()'s main role is to set a Toolbar to act as the ActionBar for an Activity's window.
This method was added in API level 25.1.0 when
ToolBar was first introduced in android framework.
setSupportActionBar expects a ToolBar object:
If you set a non-null value to the
setSupportActionBar(), invoking the
getActionBar() method will return an ActionBar object that can be used to control the given toolbar as if it were a traditional window decor action bar.
The toolbar's menu will be populated with the Activity's options menu and the navigation button will be wired through the standard home menu select action.
In order to use a Toolbar within the Activity's window content the application must not request the window feature