Android APIs
public class

Toolbar

extends ViewGroup
java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.support.v7.widget.Toolbar

Class Overview

A standard toolbar for use within application content.

A Toolbar is a generalization of action bars for use within application layouts. While an action bar is traditionally part of an Activity's opaque window decor controlled by the framework, a Toolbar may be placed at any arbitrary level of nesting within a view hierarchy. An application may choose to designate a Toolbar as the action bar for an Activity using the setSupportActionBar() method.

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. 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.
  • 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 Toolbar.LayoutParams indicates a Gravity value of CENTER_HORIZONTAL the 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.

In modern Android UIs developers should lean more on a visually distinct color scheme for toolbars than on their application icon. The use of application icon plus title as a standard layout is discouraged on API 21 devices and newer.

Summary

Nested Classes
class Toolbar.LayoutParams Layout information for child views of Toolbars. 
interface Toolbar.OnMenuItemClickListener Interface responsible for receiving menu item click events if the items themselves do not have individual item click listeners. 
[Expand]
Inherited XML Attributes
From class android.view.ViewGroup
From class android.view.View
[Expand]
Inherited Constants
From class android.view.ViewGroup
From class android.view.View
[Expand]
Inherited Fields
From class android.view.View
Public Constructors
Toolbar(Context context)
Toolbar(Context context, AttributeSet attrs)
Toolbar(Context context, AttributeSet attrs, int defStyleAttr)
Public Methods
void collapseActionView()
Collapse a currently expanded action view.
void dismissPopupMenus()
Dismiss all currently showing popup menus, including overflow or submenus.
Toolbar.LayoutParams generateLayoutParams(AttributeSet attrs)
Returns a new set of layout parameters based on the supplied attributes set.
int getContentInsetEnd()
Get the ending content inset for this toolbar.
int getContentInsetLeft()
Get the left content inset for this toolbar.
int getContentInsetRight()
Get the right content inset for this toolbar.
int getContentInsetStart()
Get the starting content inset for this toolbar.
Drawable getLogo()
Return the current logo drawable.
CharSequence getLogoDescription()
Return the description of the toolbar's logo.
Menu getMenu()
Return the Menu shown in the toolbar.
CharSequence getNavigationContentDescription()
Retrieve the currently configured content description for the navigation button view.
Drawable getNavigationIcon()
Return the current drawable used as the navigation icon.
int getPopupTheme()
CharSequence getSubtitle()
Return the subtitle of this toolbar.
CharSequence getTitle()
Returns the title of this toolbar.
boolean hasExpandedActionView()
Check whether this Toolbar is currently hosting an expanded action view.
boolean hideOverflowMenu()
Hide the overflow items from the associated menu.
void inflateMenu(int resId)
Inflate a menu resource into this toolbar.
boolean isOverflowMenuShowing()
Check whether the overflow menu is currently showing.
boolean onHoverEvent(MotionEvent ev)
Implement this method to handle hover events.
void onRtlPropertiesChanged(int layoutDirection)
Called when any RTL property (layout direction or text direction or text alignment) has been changed.
boolean onTouchEvent(MotionEvent ev)
Implement this method to handle touch screen motion events.
void setContentInsetsAbsolute(int contentInsetLeft, int contentInsetRight)
Set the content insets for this toolbar.
void setContentInsetsRelative(int contentInsetStart, int contentInsetEnd)
Set the content insets for this toolbar relative to layout direction.
void setLogo(int resId)
Set a logo drawable from a resource id.
void setLogo(Drawable drawable)
Set a logo drawable.
void setLogoDescription(int resId)
Set a description of the toolbar's logo.
void setLogoDescription(CharSequence description)
Set a description of the toolbar's logo.
void setMinimumHeight(int minHeight)
Sets the minimum height of the view.
void setNavigationContentDescription(int resId)
Set a content description for the navigation button if one is present.
void setNavigationContentDescription(CharSequence description)
Set a content description for the navigation button if one is present.
void setNavigationIcon(Drawable icon)
Set the icon to use for the toolbar's navigation button.
void setNavigationIcon(int resId)
Set the icon to use for the toolbar's navigation button.
void setNavigationOnClickListener(View.OnClickListener listener)
Set a listener to respond to navigation events.
void setOnMenuItemClickListener(Toolbar.OnMenuItemClickListener listener)
Set a listener to respond to menu item click events.
void setPopupTheme(int resId)
Specifies the theme to use when inflating popup menus.
void setSubtitle(int resId)
Set the subtitle of this toolbar.
void setSubtitle(CharSequence subtitle)
Set the subtitle of this toolbar.
void setSubtitleTextAppearance(Context context, int resId)
Sets the text color, size, style, hint color, and highlight color from the specified TextAppearance resource.
void setSubtitleTextColor(int color)
Sets the text color of the subtitle, if present.
void setTitle(int resId)
Set the title of this toolbar.
void setTitle(CharSequence title)
Set the title of this toolbar.
void setTitleTextAppearance(Context context, int resId)
Sets the text color, size, style, hint color, and highlight color from the specified TextAppearance resource.
void setTitleTextColor(int color)
Sets the text color of the title, if present.
boolean showOverflowMenu()
Show the overflow items from the associated menu.
Protected Methods
boolean checkLayoutParams(ViewGroup.LayoutParams p)
Toolbar.LayoutParams generateDefaultLayoutParams()
Returns a set of default layout parameters.
Toolbar.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p)
Returns a safe set of layout parameters based on the supplied layout params.
void onDetachedFromWindow()
This is called when the view is detached from a window.
void onLayout(boolean changed, int l, int t, int r, int b)
Called from layout when this view should assign a size and position to each of its children.
void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

Measure the view and its content to determine the measured width and the measured height.

void onRestoreInstanceState(Parcelable state)
Hook allowing a view to re-apply a representation of its internal state that had previously been generated by onSaveInstanceState().
Parcelable onSaveInstanceState()
Hook allowing a view to generate a representation of its internal state that can later be used to create a new instance with that same state.
[Expand]
Inherited Methods
From class android.view.ViewGroup
From class android.view.View
From class java.lang.Object
From interface android.view.ViewParent
From interface android.view.ViewManager
From interface android.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.accessibility.AccessibilityEventSource

Public Constructors

public Toolbar (Context context)

public Toolbar (Context context, AttributeSet attrs)

public Toolbar (Context context, AttributeSet attrs, int defStyleAttr)

Public Methods

public void collapseActionView ()

Collapse a currently expanded action view. If this Toolbar does not have an expanded action view this method has no effect.

An action view may be expanded either directly from the MenuItem it belongs to or by user action.

public void dismissPopupMenus ()

Dismiss all currently showing popup menus, including overflow or submenus.

public Toolbar.LayoutParams generateLayoutParams (AttributeSet attrs)

Returns a new set of layout parameters based on the supplied attributes set.

Parameters
attrs the attributes to build the layout parameters from
Returns

public int getContentInsetEnd ()

Get the ending content inset for this toolbar.

The content inset affects the valid area for Toolbar content other than the navigation button and menu. Insets define the minimum margin for these components and can be used to effectively align Toolbar content along well-known gridlines.

Returns
  • The ending content inset for this toolbar

public int getContentInsetLeft ()

Get the left content inset for this toolbar.

The content inset affects the valid area for Toolbar content other than the navigation button and menu. Insets define the minimum margin for these components and can be used to effectively align Toolbar content along well-known gridlines.

Returns
  • The left content inset for this toolbar

public int getContentInsetRight ()

Get the right content inset for this toolbar.

The content inset affects the valid area for Toolbar content other than the navigation button and menu. Insets define the minimum margin for these components and can be used to effectively align Toolbar content along well-known gridlines.

Returns
  • The right content inset for this toolbar

public int getContentInsetStart ()

Get the starting content inset for this toolbar.

The content inset affects the valid area for Toolbar content other than the navigation button and menu. Insets define the minimum margin for these components and can be used to effectively align Toolbar content along well-known gridlines.

Returns
  • The starting content inset for this toolbar

public Drawable getLogo ()

Return the current logo drawable.

Returns
  • The current logo drawable

public CharSequence getLogoDescription ()

Return the description of the toolbar's logo.

Returns
  • A description of the logo

public Menu getMenu ()

Return the Menu shown in the toolbar.

Applications that wish to populate the toolbar's menu can do so from here. To use an XML menu resource, use inflateMenu(int).

Returns
  • The toolbar's Menu

public CharSequence getNavigationContentDescription ()

Retrieve the currently configured content description for the navigation button view. This will be used to describe the navigation action to users through mechanisms such as screen readers or tooltips.

Returns
  • The navigation button's content description

public Drawable getNavigationIcon ()

Return the current drawable used as the navigation icon.

Returns
  • The navigation icon drawable

public int getPopupTheme ()

Returns
  • resource identifier of the theme used to inflate popup menus, or 0 if menus are inflated against the toolbar theme

public CharSequence getSubtitle ()

Return the subtitle of this toolbar.

Returns
  • The current subtitle

public CharSequence getTitle ()

Returns the title of this toolbar.

Returns
  • The current title.

public boolean hasExpandedActionView ()

Check whether this Toolbar is currently hosting an expanded action view.

An action view may be expanded either directly from the MenuItem it belongs to or by user action. If the Toolbar has an expanded action view it can be collapsed using the collapseActionView() method.

Returns
  • true if the Toolbar has an expanded action view

public boolean hideOverflowMenu ()

Hide the overflow items from the associated menu.

Returns
  • true if the menu was able to be hidden, false otherwise

public void inflateMenu (int resId)

Inflate a menu resource into this toolbar.

Inflate an XML menu resource into this toolbar. Existing items in the menu will not be modified or removed.

Parameters
resId ID of a menu resource to inflate

public boolean isOverflowMenuShowing ()

Check whether the overflow menu is currently showing. This may not reflect a pending show operation in progress.

Returns
  • true if the overflow menu is currently showing

public boolean onHoverEvent (MotionEvent ev)

Implement this method to handle hover events.

This method is called whenever a pointer is hovering into, over, or out of the bounds of a view and the view is not currently being touched. Hover events are represented as pointer events with action ACTION_HOVER_ENTER, ACTION_HOVER_MOVE, or ACTION_HOVER_EXIT.

  • The view receives a hover event with action ACTION_HOVER_ENTER when the pointer enters the bounds of the view.
  • The view receives a hover event with action ACTION_HOVER_MOVE when the pointer has already entered the bounds of the view and has moved.
  • The view receives a hover event with action ACTION_HOVER_EXIT when the pointer has exited the bounds of the view or when the pointer is about to go down due to a button click, tap, or similar user action that causes the view to be touched.

The view should implement this method to return true to indicate that it is handling the hover event, such as by changing its drawable state.

The default implementation calls setHovered(boolean) to update the hovered state of the view when a hover enter or hover exit event is received, if the view is enabled and is clickable. The default implementation also sends hover accessibility events.

Parameters
ev The motion event that describes the hover.
Returns
  • True if the view handled the hover event.

public void onRtlPropertiesChanged (int layoutDirection)

Called when any RTL property (layout direction or text direction or text alignment) has been changed. Subclasses need to override this method to take care of cached information that depends on the resolved layout direction, or to inform child views that inherit their layout direction. The default implementation does nothing.

Parameters
layoutDirection the direction of the layout

public boolean onTouchEvent (MotionEvent ev)

Implement this method to handle touch screen motion events.

If this method is used to detect click actions, it is recommended that the actions be performed by implementing and calling performClick(). This will ensure consistent system behavior, including:

  • obeying click sound preferences
  • dispatching OnClickListener calls
  • handling ACTION_CLICK when accessibility features are enabled

Parameters
ev The motion event.
Returns
  • True if the event was handled, false otherwise.

public void setContentInsetsAbsolute (int contentInsetLeft, int contentInsetRight)

Set the content insets for this toolbar.

The content inset affects the valid area for Toolbar content other than the navigation button and menu. Insets define the minimum margin for these components and can be used to effectively align Toolbar content along well-known gridlines.

Parameters
contentInsetLeft Content inset for the toolbar's left edge
contentInsetRight Content inset for the toolbar's right edge

public void setContentInsetsRelative (int contentInsetStart, int contentInsetEnd)

Set the content insets for this toolbar relative to layout direction.

The content inset affects the valid area for Toolbar content other than the navigation button and menu. Insets define the minimum margin for these components and can be used to effectively align Toolbar content along well-known gridlines.

Parameters
contentInsetStart Content inset for the toolbar starting edge
contentInsetEnd Content inset for the toolbar ending edge

public void setLogo (int resId)

Set a logo drawable from a resource id.

This drawable should generally take the place of title text. The logo cannot be clicked. Apps using a logo should also supply a description using setLogoDescription(int).

Parameters
resId ID of a drawable resource

public void setLogo (Drawable drawable)

Set a logo drawable.

This drawable should generally take the place of title text. The logo cannot be clicked. Apps using a logo should also supply a description using setLogoDescription(int).

Parameters
drawable Drawable to use as a logo

public void setLogoDescription (int resId)

Set a description of the toolbar's logo.

This description will be used for accessibility or other similar descriptions of the UI.

Parameters
resId String resource id

public void setLogoDescription (CharSequence description)

Set a description of the toolbar's logo.

This description will be used for accessibility or other similar descriptions of the UI.

Parameters
description Description to set

public void setMinimumHeight (int minHeight)

Sets the minimum height of the view. It is not guaranteed the view will be able to achieve this minimum height (for example, if its parent layout constrains it with less available height).

Parameters
minHeight The minimum height the view will try to be.

public void setNavigationContentDescription (int resId)

Set a content description for the navigation button if one is present. The content description will be read via screen readers or other accessibility systems to explain the action of the navigation button.

Parameters
resId Resource ID of a content description string to set, or 0 to clear the description

public void setNavigationContentDescription (CharSequence description)

Set a content description for the navigation button if one is present. The content description will be read via screen readers or other accessibility systems to explain the action of the navigation button.

Parameters
description Content description to set, or null to clear the content description

public void setNavigationIcon (Drawable icon)

Set the icon to use for the toolbar's navigation button.

The navigation button appears at the start of the toolbar if present. Setting an icon will make the navigation button visible.

If you use a navigation icon you should also set a description for its action using setNavigationContentDescription(int). This is used for accessibility and tooltips.

Parameters
icon Drawable to set, may be null to clear the icon

public void setNavigationIcon (int resId)

Set the icon to use for the toolbar's navigation button.

The navigation button appears at the start of the toolbar if present. Setting an icon will make the navigation button visible.

If you use a navigation icon you should also set a description for its action using setNavigationContentDescription(int). This is used for accessibility and tooltips.

Parameters
resId Resource ID of a drawable to set

public void setNavigationOnClickListener (View.OnClickListener listener)

Set a listener to respond to navigation events.

This listener will be called whenever the user clicks the navigation button at the start of the toolbar. An icon must be set for the navigation button to appear.

Parameters
listener Listener to set

public void setOnMenuItemClickListener (Toolbar.OnMenuItemClickListener listener)

Set a listener to respond to menu item click events.

This listener will be invoked whenever a user selects a menu item from the action buttons presented at the end of the toolbar or the associated overflow.

Parameters
listener Listener to set

public void setPopupTheme (int resId)

Specifies the theme to use when inflating popup menus. By default, uses the same theme as the toolbar itself.

Parameters
resId theme used to inflate popup menus
See Also

public void setSubtitle (int resId)

Set the subtitle of this toolbar.

Subtitles should express extended information about the current content.

Parameters
resId String resource ID

public void setSubtitle (CharSequence subtitle)

Set the subtitle of this toolbar.

Subtitles should express extended information about the current content.

Parameters
subtitle Subtitle to set

public void setSubtitleTextAppearance (Context context, int resId)

Sets the text color, size, style, hint color, and highlight color from the specified TextAppearance resource.

public void setSubtitleTextColor (int color)

Sets the text color of the subtitle, if present.

Parameters
color The new text color in 0xAARRGGBB format

public void setTitle (int resId)

Set the title of this toolbar.

A title should be used as the anchor for a section of content. It should describe or name the content being viewed.

Parameters
resId Resource ID of a string to set as the title

public void setTitle (CharSequence title)

Set the title of this toolbar.

A title should be used as the anchor for a section of content. It should describe or name the content being viewed.

Parameters
title Title to set

public void setTitleTextAppearance (Context context, int resId)

Sets the text color, size, style, hint color, and highlight color from the specified TextAppearance resource.

public void setTitleTextColor (int color)

Sets the text color of the title, if present.

Parameters
color The new text color in 0xAARRGGBB format

public boolean showOverflowMenu ()

Show the overflow items from the associated menu.

Returns
  • true if the menu was able to be shown, false otherwise

Protected Methods

protected boolean checkLayoutParams (ViewGroup.LayoutParams p)

protected Toolbar.LayoutParams generateDefaultLayoutParams ()

Returns a set of default layout parameters. These parameters are requested when the View passed to addView(View) has no layout parameters already set. If null is returned, an exception is thrown from addView.

Returns
  • a set of default layout parameters or null

protected Toolbar.LayoutParams generateLayoutParams (ViewGroup.LayoutParams p)

Returns a safe set of layout parameters based on the supplied layout params. When a ViewGroup is passed a View whose layout params do not pass the test of checkLayoutParams(android.view.ViewGroup.LayoutParams), this method is invoked. This method should return a new set of layout params suitable for this ViewGroup, possibly by copying the appropriate attributes from the specified set of layout params.

Parameters
p The layout parameters to convert into a suitable set of layout parameters for this ViewGroup.
Returns

protected void onDetachedFromWindow ()

This is called when the view is detached from a window. At this point it no longer has a surface for drawing.

protected void onLayout (boolean changed, int l, int t, int r, int b)

Called from layout when this view should assign a size and position to each of its children. Derived classes with children should override this method and call layout on each of their children.

Parameters
changed This is a new size or position for this view
l Left position, relative to parent
t Top position, relative to parent
r Right position, relative to parent
b Bottom position, relative to parent

protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)

Measure the view and its content to determine the measured width and the measured height. This method is invoked by measure(int, int) and should be overriden by subclasses to provide accurate and efficient measurement of their contents.

CONTRACT: When overriding this method, you must call setMeasuredDimension(int, int) to store the measured width and height of this view. Failure to do so will trigger an IllegalStateException, thrown by measure(int, int). Calling the superclass' onMeasure(int, int) is a valid use.

The base class implementation of measure defaults to the background size, unless a larger size is allowed by the MeasureSpec. Subclasses should override onMeasure(int, int) to provide better measurements of their content.

If this method is overridden, it is the subclass's responsibility to make sure the measured height and width are at least the view's minimum height and width (getSuggestedMinimumHeight() and getSuggestedMinimumWidth()).

Parameters
widthMeasureSpec horizontal space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.
heightMeasureSpec vertical space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.

protected void onRestoreInstanceState (Parcelable state)

Hook allowing a view to re-apply a representation of its internal state that had previously been generated by onSaveInstanceState(). This function will never be called with a null state.

Parameters
state The frozen state that had previously been returned by onSaveInstanceState().

protected Parcelable onSaveInstanceState ()

Hook allowing a view to generate a representation of its internal state that can later be used to create a new instance with that same state. This state should only contain information that is not persistent or can not be reconstructed later. For example, you will never store your current position on screen because that will be computed again when a new instance of the view is placed in its view hierarchy.

Some examples of things you may store here: the current cursor position in a text view (but usually not the text itself since that is stored in a content provider or other persistent storage), the currently selected item in a list view.

Returns
  • Returns a Parcelable object containing the view's current dynamic state, or null if there is nothing interesting to save. The default implementation returns null.