Toolbar
public
class
Toolbar
extends ViewGroup
java.lang.Object | |||
↳ | android.view.View | ||
↳ | android.view.ViewGroup | ||
↳ | android.widget.Toolbar |
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 setActionBar()
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.
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
LayoutParams
indicates aGravity
value ofCENTER_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. Action buttons are vertically aligned within the Toolbar'sminimum height
, if set.
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. |
XML attributes | ||
---|---|---|
android:buttonGravity |
||
android:collapseContentDescription |
Text to set as the content description for the collapse button. | |
android:collapseIcon |
Icon drawable to use for the collapse button. | |
android:contentInsetEnd |
Minimum inset for content views within a bar. | |
android:contentInsetEndWithActions |
Minimum inset for content views within a bar when actions from a menu are present. | |
android:contentInsetLeft |
Minimum inset for content views within a bar. | |
android:contentInsetRight |
Minimum inset for content views within a bar. | |
android:contentInsetStart |
Minimum inset for content views within a bar. | |
android:contentInsetStartWithNavigation |
Minimum inset for content views within a bar when a navigation button is present, such as the Up button. | |
android:gravity |
Specifies how an object should position its content, on both the X and Y axes, within its own bounds. | |
android:logo |
Drawable to set as the logo that appears at the starting side of the Toolbar, just after the navigation button. | |
android:logoDescription |
A content description string to describe the appearance of the associated logo image. | |
android:maxButtonHeight |
||
android:navigationContentDescription |
Text to set as the content description for the navigation button located at the start of the toolbar. | |
android:navigationIcon |
Icon drawable to use for the navigation button located at the start of the toolbar. | |
android:popupTheme |
Reference to a theme that should be used to inflate popups shown by widgets in the toolbar. | |
android:subtitle |
Specifies subtitle text used for navigationMode="normal". | |
android:subtitleTextAppearance |
||
android:subtitleTextColor |
A color to apply to the subtitle string. | |
android:title |
The title associated with the item. | |
android:titleMargin |
Specifies extra space on the left, start, right and end sides of the toolbar's title. | |
android:titleMarginBottom |
Specifies extra space on the bottom side of the toolbar's title. | |
android:titleMarginEnd |
Specifies extra space on the end side of the toolbar's title. | |
android:titleMarginStart |
Specifies extra space on the start side of the toolbar's title. | |
android:titleMarginTop |
Specifies extra space on the top side of the toolbar's title. | |
android:titleTextAppearance |
||
android:titleTextColor |
A color to apply to the title string. |
Inherited XML attributes | |
---|---|
Inherited constants |
---|
Inherited fields |
---|
Public constructors | |
---|---|
Toolbar(Context context)
|
|
Toolbar(Context context, AttributeSet attrs)
|
|
Toolbar(Context context, AttributeSet attrs, int defStyleAttr)
|
|
Toolbar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
|
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. |
CharSequence
|
getCollapseContentDescription()
Retrieve the currently configured content description for the collapse button view. |
Drawable
|
getCollapseIcon()
Return the current drawable used as the collapse icon. |
int
|
getContentInsetEnd()
Gets the ending content inset for this toolbar. |
int
|
getContentInsetEndWithActions()
Gets the end content inset to use when action buttons are present. |
int
|
getContentInsetLeft()
Gets the left content inset for this toolbar. |
int
|
getContentInsetRight()
Gets the right content inset for this toolbar. |
int
|
getContentInsetStart()
Gets the starting content inset for this toolbar. |
int
|
getContentInsetStartWithNavigation()
Gets the start content inset to use when a navigation button is present. |
int
|
getCurrentContentInsetEnd()
Gets the content inset that will be used on the ending side of the bar in the current toolbar configuration. |
int
|
getCurrentContentInsetLeft()
Gets the content inset that will be used on the left side of the bar in the current toolbar configuration. |
int
|
getCurrentContentInsetRight()
Gets the content inset that will be used on the right side of the bar in the current toolbar configuration. |
int
|
getCurrentContentInsetStart()
Gets the content inset that will be used on the starting side of the bar in the current toolbar configuration. |
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. |
Drawable
|
getOverflowIcon()
Return the current drawable used as the overflow icon. |
int
|
getPopupTheme()
|
CharSequence
|
getSubtitle()
Return the subtitle of this toolbar. |
CharSequence
|
getTitle()
Returns the title of this toolbar. |
int
|
getTitleMarginBottom()
|
int
|
getTitleMarginEnd()
|
int
|
getTitleMarginStart()
|
int
|
getTitleMarginTop()
|
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. |
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 pointer events. |
void
|
setCollapseContentDescription(CharSequence description)
Set a content description for the collapse button if one is present. |
void
|
setCollapseContentDescription(int resId)
Set a content description for the collapse button if one is present. |
void
|
setCollapseIcon(Drawable icon)
Set the icon to use for the toolbar's collapse button. |
void
|
setCollapseIcon(int resId)
Set the icon to use for the toolbar's collapse button. |
void
|
setContentInsetEndWithActions(int insetEndWithActions)
Sets the start content inset to use when action buttons are present. |
void
|
setContentInsetStartWithNavigation(int insetStartWithNavigation)
Sets the start content inset to use when a navigation button is present. |
void
|
setContentInsetsAbsolute(int contentInsetLeft, int contentInsetRight)
Sets the content insets for this toolbar. |
void
|
setContentInsetsRelative(int contentInsetStart, int contentInsetEnd)
Sets 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
|
setNavigationContentDescription(CharSequence description)
Set a content description for the navigation button if one is present. |
void
|
setNavigationContentDescription(int resId)
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
|
setOverflowIcon(Drawable icon)
Set the icon to use for the overflow button. |
void
|
setPopupTheme(int resId)
Specifies the theme to use when inflating popup menus. |
void
|
setSubtitle(CharSequence subtitle)
Set the subtitle of this toolbar. |
void
|
setSubtitle(int resId)
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(CharSequence title)
Set the title of this toolbar. |
void
|
setTitle(int resId)
Set the title of this toolbar. |
void
|
setTitleMargin(int start, int top, int end, int bottom)
Sets the title margin. |
void
|
setTitleMarginBottom(int margin)
Sets the bottom title margin in pixels. |
void
|
setTitleMarginEnd(int margin)
Sets the ending title margin in pixels. |
void
|
setTitleMarginStart(int margin)
Sets the starting title margin in pixels. |
void
|
setTitleMarginTop(int margin)
Sets the top title margin in pixels. |
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
|
onAttachedToWindow()
This is called when the view is attached to a window. |
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 |
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. |
Inherited methods | |
---|---|
XML attributes
android:buttonGravity
android:collapseContentDescription
Text to set as the content description for the collapse button.
May be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character;
Related methods:
android:collapseIcon
Icon drawable to use for the collapse button.
May be a reference to another resource, in the form
"@[+][package:]type/name
" or a theme
attribute in the form
"?[package:]type/name
".
Related methods:
android:contentInsetEnd
Minimum inset for content views within a bar. Navigation buttons and menu views are excepted. Only valid for some themes and configurations.
May be a dimension value, which is a floating point number appended with a
unit such as "14.5sp
".
Available units are: px (pixels), dp (density-independent pixels),
sp (scaled pixels based on preferred font size), in (inches), and
mm (millimeters).
Related methods:
android:contentInsetEndWithActions
Minimum inset for content views within a bar when actions from a menu are present. Only valid for some themes and configurations.
May be a dimension value, which is a floating point number appended with a
unit such as "14.5sp
".
Available units are: px (pixels), dp (density-independent pixels),
sp (scaled pixels based on preferred font size), in (inches), and
mm (millimeters).
Related methods:
android:contentInsetLeft
Minimum inset for content views within a bar. Navigation buttons and menu views are excepted. Only valid for some themes and configurations.
May be a dimension value, which is a floating point number appended with a
unit such as "14.5sp
".
Available units are: px (pixels), dp (density-independent pixels),
sp (scaled pixels based on preferred font size), in (inches), and
mm (millimeters).
Related methods:
android:contentInsetRight
Minimum inset for content views within a bar. Navigation buttons and menu views are excepted. Only valid for some themes and configurations.
May be a dimension value, which is a floating point number appended with a
unit such as "14.5sp
".
Available units are: px (pixels), dp (density-independent pixels),
sp (scaled pixels based on preferred font size), in (inches), and
mm (millimeters).
Related methods:
android:contentInsetStart
Minimum inset for content views within a bar. Navigation buttons and menu views are excepted. Only valid for some themes and configurations.
May be a dimension value, which is a floating point number appended with a
unit such as "14.5sp
".
Available units are: px (pixels), dp (density-independent pixels),
sp (scaled pixels based on preferred font size), in (inches), and
mm (millimeters).
Related methods:
android:contentInsetStartWithNavigation
Minimum inset for content views within a bar when a navigation button is present, such as the Up button. Only valid for some themes and configurations.
May be a dimension value, which is a floating point number appended with a
unit such as "14.5sp
".
Available units are: px (pixels), dp (density-independent pixels),
sp (scaled pixels based on preferred font size), in (inches), and
mm (millimeters).
Related methods:
android:gravity
Specifies how an object should position its content, on both the X and Y axes, within its own bounds.
Must be one or more (separated by '|') of the following constant values.
Constant | Value | Description |
---|---|---|
bottom | 50 | Push object to the bottom of its container, not changing its size. |
center | 11 | Place the object in the center of its container in both the vertical and horizontal axis, not changing its size. |
center_horizontal | 1 | Place object in the horizontal center of its container, not changing its size. |
center_vertical | 10 | Place object in the vertical center of its container, not changing its size. |
clip_horizontal | 8 | Additional option that can be set to have the left and/or right edges of the child clipped to its container's bounds. The clip will be based on the horizontal gravity: a left gravity will clip the right edge, a right gravity will clip the left edge, and neither will clip both edges. |
clip_vertical | 80 | Additional option that can be set to have the top and/or bottom edges of the child clipped to its container's bounds. The clip will be based on the vertical gravity: a top gravity will clip the bottom edge, a bottom gravity will clip the top edge, and neither will clip both edges. |
end | 800005 | Push object to the end of its container, not changing its size. |
fill | 77 | Grow the horizontal and vertical size of the object if needed so it completely fills its container. |
fill_horizontal | 7 | Grow the horizontal size of the object if needed so it completely fills its container. |
fill_vertical | 70 | Grow the vertical size of the object if needed so it completely fills its container. |
left | 3 | Push object to the left of its container, not changing its size. |
right | 5 | Push object to the right of its container, not changing its size. |
start | 800003 | Push object to the beginning of its container, not changing its size. |
top | 30 | Push object to the top of its container, not changing its size. |
android:logo
Drawable to set as the logo that appears at the starting side of the Toolbar, just after the navigation button.
May be a reference to another resource, in the form
"@[+][package:]type/name
" or a theme
attribute in the form
"?[package:]type/name
".
android:logoDescription
A content description string to describe the appearance of the associated logo image.
May be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character;
android:maxButtonHeight
android:navigationContentDescription
Text to set as the content description for the navigation button located at the start of the toolbar.
May be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character;
Related methods:
android:navigationIcon
Icon drawable to use for the navigation button located at the start of the toolbar.
May be a reference to another resource, in the form
"@[+][package:]type/name
" or a theme
attribute in the form
"?[package:]type/name
".
Related methods:
android:popupTheme
Reference to a theme that should be used to inflate popups shown by widgets in the toolbar.
May be a reference to another resource, in the form
"@[+][package:]type/name
" or a theme
attribute in the form
"?[package:]type/name
".
android:subtitle
Specifies subtitle text used for navigationMode="normal".
May be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character;
android:subtitleTextAppearance
android:subtitleTextColor
A color to apply to the subtitle string.
May be a color value, in the form of "#rgb
",
"#argb
", "#rrggbb
", or
"#aarrggbb
".
android:title
The title associated with the item.
May be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character;
android:titleMargin
Specifies extra space on the left, start, right and end sides of the toolbar's title. Margin values should be positive.
May be a dimension value, which is a floating point number appended with a
unit such as "14.5sp
".
Available units are: px (pixels), dp (density-independent pixels),
sp (scaled pixels based on preferred font size), in (inches), and
mm (millimeters).
Related methods:
android:titleMarginBottom
Specifies extra space on the bottom side of the toolbar's title. If both this attribute and titleMargin are specified, then this attribute takes precedence. Margin values should be positive.
May be a dimension value, which is a floating point number appended with a
unit such as "14.5sp
".
Available units are: px (pixels), dp (density-independent pixels),
sp (scaled pixels based on preferred font size), in (inches), and
mm (millimeters).
Related methods:
android:titleMarginEnd
Specifies extra space on the end side of the toolbar's title. If both this attribute and titleMargin are specified, then this attribute takes precedence. Margin values should be positive.
May be a dimension value, which is a floating point number appended with a
unit such as "14.5sp
".
Available units are: px (pixels), dp (density-independent pixels),
sp (scaled pixels based on preferred font size), in (inches), and
mm (millimeters).
Related methods:
android:titleMarginStart
Specifies extra space on the start side of the toolbar's title. If both this attribute and titleMargin are specified, then this attribute takes precedence. Margin values should be positive.
May be a dimension value, which is a floating point number appended with a
unit such as "14.5sp
".
Available units are: px (pixels), dp (density-independent pixels),
sp (scaled pixels based on preferred font size), in (inches), and
mm (millimeters).
Related methods:
android:titleMarginTop
Specifies extra space on the top side of the toolbar's title. If both this attribute and titleMargin are specified, then this attribute takes precedence. Margin values should be positive.
May be a dimension value, which is a floating point number appended with a
unit such as "14.5sp
".
Available units are: px (pixels), dp (density-independent pixels),
sp (scaled pixels based on preferred font size), in (inches), and
mm (millimeters).
Related methods:
android:titleTextAppearance
android:titleTextColor
A color to apply to the title string.
May be a color value, in the form of "#rgb
",
"#argb
", "#rrggbb
", or
"#aarrggbb
".
Public constructors
Toolbar
public Toolbar (Context context, AttributeSet attrs)
Parameters | |
---|---|
context |
Context |
attrs |
AttributeSet |
Toolbar
public Toolbar (Context context, AttributeSet attrs, int defStyleAttr)
Parameters | |
---|---|
context |
Context |
attrs |
AttributeSet |
defStyleAttr |
int |
Toolbar
public Toolbar (Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
Parameters | |
---|---|
context |
Context |
attrs |
AttributeSet |
defStyleAttr |
int |
defStyleRes |
int |
Public methods
collapseActionView
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.
See also:
dismissPopupMenus
public void dismissPopupMenus ()
Dismiss all currently showing popup menus, including overflow or submenus.
generateLayoutParams
public Toolbar.LayoutParams generateLayoutParams (AttributeSet attrs)
Returns a new set of layout parameters based on the supplied attributes set.
Parameters | |
---|---|
attrs |
AttributeSet : the attributes to build the layout parameters from |
Returns | |
---|---|
Toolbar.LayoutParams |
an instance of ViewGroup.LayoutParams or one
of its descendants |
getCollapseContentDescription
public CharSequence getCollapseContentDescription ()
Retrieve the currently configured content description for the collapse button view. This will be used to describe the collapse action to users through mechanisms such as screen readers or tooltips.
Related XML Attributes:
Returns | |
---|---|
CharSequence |
The collapse button's content description
This value may be null . |
getCollapseIcon
public Drawable getCollapseIcon ()
Return the current drawable used as the collapse icon.
Related XML Attributes:
Returns | |
---|---|
Drawable |
The collapse icon drawable
This value may be null . |
getContentInsetEnd
public int getContentInsetEnd ()
Gets 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.
Related XML Attributes:
Returns | |
---|---|
int |
The ending content inset for this toolbar |
getContentInsetEndWithActions
public int getContentInsetEndWithActions ()
Gets the end content inset to use when action buttons are present.
Different content insets are often called for when additional buttons are present
in the toolbar, as well as at different toolbar sizes. The larger value of
getContentInsetEnd()
and this value will be used during layout.
Related XML Attributes:
Returns | |
---|---|
int |
the end content inset used when a menu has been set in pixels |
See also:
getContentInsetLeft
public int getContentInsetLeft ()
Gets 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.
Related XML Attributes:
Returns | |
---|---|
int |
The left content inset for this toolbar |
getContentInsetRight
public int getContentInsetRight ()
Gets 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.
Related XML Attributes:
Returns | |
---|---|
int |
The right content inset for this toolbar |
getContentInsetStart
public int getContentInsetStart ()
Gets 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.
Related XML Attributes:
Returns | |
---|---|
int |
The starting content inset for this toolbar |
getContentInsetStartWithNavigation
public int getContentInsetStartWithNavigation ()
Gets the start content inset to use when a navigation button is present.
Different content insets are often called for when additional buttons are present
in the toolbar, as well as at different toolbar sizes. The larger value of
getContentInsetStart()
and this value will be used during layout.
Related XML Attributes:
Returns | |
---|---|
int |
the start content inset used when a navigation icon has been set in pixels |
See also:
getCurrentContentInsetEnd
public int getCurrentContentInsetEnd ()
Gets the content inset that will be used on the ending side of the bar in the current toolbar configuration.
Returns | |
---|---|
int |
the current content inset end in pixels |
See also:
getCurrentContentInsetLeft
public int getCurrentContentInsetLeft ()
Gets the content inset that will be used on the left side of the bar in the current toolbar configuration.
Returns | |
---|---|
int |
the current content inset left in pixels |
getCurrentContentInsetRight
public int getCurrentContentInsetRight ()
Gets the content inset that will be used on the right side of the bar in the current toolbar configuration.
Returns | |
---|---|
int |
the current content inset right in pixels |
getCurrentContentInsetStart
public int getCurrentContentInsetStart ()
Gets the content inset that will be used on the starting side of the bar in the current toolbar configuration.
Returns | |
---|---|
int |
the current content inset start in pixels |
See also:
getLogo
public Drawable getLogo ()
Return the current logo drawable.
Returns | |
---|---|
Drawable |
The current logo drawable |
getLogoDescription
public CharSequence getLogoDescription ()
Return the description of the toolbar's logo.
Returns | |
---|---|
CharSequence |
A description of the logo |
getMenu
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 | |
---|---|
Menu |
The toolbar's Menu |
getNavigationContentDescription
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.
Related XML Attributes:
Returns | |
---|---|
CharSequence |
The navigation button's content description
This value may be null . |
getNavigationIcon
public Drawable getNavigationIcon ()
Return the current drawable used as the navigation icon.
Related XML Attributes:
Returns | |
---|---|
Drawable |
The navigation icon drawable
This value may be null . |
getOverflowIcon
public Drawable getOverflowIcon ()
Return the current drawable used as the overflow icon.
Returns | |
---|---|
Drawable |
The overflow icon drawable
This value may be null . |
getPopupTheme
public int getPopupTheme ()
Returns | |
---|---|
int |
resource identifier of the theme used to inflate popup menus, or 0 if menus are inflated against the toolbar theme |
See also:
getSubtitle
public CharSequence getSubtitle ()
Return the subtitle of this toolbar.
Returns | |
---|---|
CharSequence |
The current subtitle |
getTitle
public CharSequence getTitle ()
Returns the title of this toolbar.
Returns | |
---|---|
CharSequence |
The current title. |
getTitleMarginBottom
public int getTitleMarginBottom ()
Related XML Attributes:
Returns | |
---|---|
int |
the bottom title margin in pixels |
See also:
getTitleMarginEnd
public int getTitleMarginEnd ()
Related XML Attributes:
Returns | |
---|---|
int |
the ending title margin in pixels |
See also:
getTitleMarginStart
public int getTitleMarginStart ()
Related XML Attributes:
Returns | |
---|---|
int |
the starting title margin in pixels |
See also:
getTitleMarginTop
public int getTitleMarginTop ()
Related XML Attributes:
Returns | |
---|---|
int |
the top title margin in pixels |
See also:
hasExpandedActionView
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 | |
---|---|
boolean |
true if the Toolbar has an expanded action view |
hideOverflowMenu
public boolean hideOverflowMenu ()
Hide the overflow items from the associated menu.
Returns | |
---|---|
boolean |
true if the menu was able to be hidden, false otherwise |
inflateMenu
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 |
int : ID of a menu resource to inflate |
isOverflowMenuShowing
public boolean isOverflowMenuShowing ()
Check whether the overflow menu is currently showing. This may not reflect a pending show operation in progress.
Returns | |
---|---|
boolean |
true if the overflow menu is currently showing |
onRtlPropertiesChanged
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 |
int : Value is View.LAYOUT_DIRECTION_LTR , or View.LAYOUT_DIRECTION_RTL |
onTouchEvent
public boolean onTouchEvent (MotionEvent ev)
Implement this method to handle pointer events.
This method is called to handle motion events where pointers are down on the view. For example, this could include touchscreen touches, stylus touches, or click-and-drag events from a mouse. However, it is not called for motion events that do not involve pointers being down, such as hover events or mouse scroll wheel movements.
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 |
MotionEvent : The motion event. |
Returns | |
---|---|
boolean |
True if the event was handled, false otherwise. |
setCollapseContentDescription
public void setCollapseContentDescription (CharSequence description)
Set a content description for the collapse 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.
Related XML Attributes:
Parameters | |
---|---|
description |
CharSequence : Content description to set, or null to
clear the content description |
setCollapseContentDescription
public void setCollapseContentDescription (int resId)
Set a content description for the collapse button if one is present. The content description will be read via screen readers or other accessibility systems to explain the action of the collapse button.
Related XML Attributes:
Parameters | |
---|---|
resId |
int : Resource ID of a content description string to set, or 0 to
clear the description |
setCollapseIcon
public void setCollapseIcon (Drawable icon)
Set the icon to use for the toolbar's collapse button.
The collapse button appears at the start of the toolbar when an action view is present .
Related XML Attributes:
Parameters | |
---|---|
icon |
Drawable : Drawable to set, may be null to use the default icon |
setCollapseIcon
public void setCollapseIcon (int resId)
Set the icon to use for the toolbar's collapse button.
The collapse button appears at the start of the toolbar when an action view is present .
Related XML Attributes:
Parameters | |
---|---|
resId |
int : Resource ID of a drawable to set |
setContentInsetEndWithActions
public void setContentInsetEndWithActions (int insetEndWithActions)
Sets the start content inset to use when action buttons are present.
Different content insets are often called for when additional buttons are present
in the toolbar, as well as at different toolbar sizes. The larger value of
getContentInsetEnd()
and this value will be used during layout.
Related XML Attributes:
Parameters | |
---|---|
insetEndWithActions |
int : the inset to use when a menu has been set in pixels |
See also:
setContentInsetStartWithNavigation
public void setContentInsetStartWithNavigation (int insetStartWithNavigation)
Sets the start content inset to use when a navigation button is present.
Different content insets are often called for when additional buttons are present
in the toolbar, as well as at different toolbar sizes. The larger value of
getContentInsetStart()
and this value will be used during layout.
Related XML Attributes:
Parameters | |
---|---|
insetStartWithNavigation |
int : the inset to use when a navigation icon has been set
in pixels |
See also:
setContentInsetsAbsolute
public void setContentInsetsAbsolute (int contentInsetLeft, int contentInsetRight)
Sets 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.
Related XML Attributes:
Parameters | |
---|---|
contentInsetLeft |
int : Content inset for the toolbar's left edge |
contentInsetRight |
int : Content inset for the toolbar's right edge |
setContentInsetsRelative
public void setContentInsetsRelative (int contentInsetStart, int contentInsetEnd)
Sets 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.
Related XML Attributes:
Parameters | |
---|---|
contentInsetStart |
int : Content inset for the toolbar starting edge |
contentInsetEnd |
int : Content inset for the toolbar ending edge |
setLogo
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 |
int : ID of a drawable resource |
setLogo
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 : Drawable to use as a logo |
setLogoDescription
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 |
int : String resource id |
setLogoDescription
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 |
CharSequence : Description to set |
setNavigationContentDescription
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.
Related XML Attributes:
Parameters | |
---|---|
description |
CharSequence : Content description to set, or null to
clear the content description |
setNavigationContentDescription
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.
Related XML Attributes:
Parameters | |
---|---|
resId |
int : Resource ID of a content description string to set, or 0 to
clear the description |
setNavigationIcon
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.
Related XML Attributes:
Parameters | |
---|---|
icon |
Drawable : Drawable to set, may be null to clear the icon |
setNavigationIcon
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.
Related XML Attributes:
Parameters | |
---|---|
resId |
int : Resource ID of a drawable to set |
setNavigationOnClickListener
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 |
View.OnClickListener : Listener to set |
setOnMenuItemClickListener
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 |
Toolbar.OnMenuItemClickListener : Listener to set |
setOverflowIcon
public void setOverflowIcon (Drawable icon)
Set the icon to use for the overflow button.
Parameters | |
---|---|
icon |
Drawable : Drawable to set, may be null to clear the icon |
setPopupTheme
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 |
int : theme used to inflate popup menus |
See also:
setSubtitle
public void setSubtitle (CharSequence subtitle)
Set the subtitle of this toolbar.
Subtitles should express extended information about the current content.
Parameters | |
---|---|
subtitle |
CharSequence : Subtitle to set |
setSubtitle
public void setSubtitle (int resId)
Set the subtitle of this toolbar.
Subtitles should express extended information about the current content.
Parameters | |
---|---|
resId |
int : String resource ID |
setSubtitleTextAppearance
public void setSubtitleTextAppearance (Context context, int resId)
Sets the text color, size, style, hint color, and highlight color from the specified TextAppearance resource.
Parameters | |
---|---|
context |
Context |
resId |
int |
setSubtitleTextColor
public void setSubtitleTextColor (int color)
Sets the text color of the subtitle, if present.
Parameters | |
---|---|
color |
int : The new text color in 0xAARRGGBB format |
setTitle
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 |
CharSequence : Title to set |
setTitle
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 |
int : Resource ID of a string to set as the title |
setTitleMargin
public void setTitleMargin (int start, int top, int end, int bottom)
Sets the title margin.
Related XML Attributes:
Parameters | |
---|---|
start |
int : the starting title margin in pixels |
top |
int : the top title margin in pixels |
end |
int : the ending title margin in pixels |
bottom |
int : the bottom title margin in pixels |
setTitleMarginBottom
public void setTitleMarginBottom (int margin)
Sets the bottom title margin in pixels.
Related XML Attributes:
Parameters | |
---|---|
margin |
int : the bottom title margin in pixels |
See also:
setTitleMarginEnd
public void setTitleMarginEnd (int margin)
Sets the ending title margin in pixels.
Related XML Attributes:
Parameters | |
---|---|
margin |
int : the ending title margin in pixels |
See also:
setTitleMarginStart
public void setTitleMarginStart (int margin)
Sets the starting title margin in pixels.
Related XML Attributes:
Parameters | |
---|---|
margin |
int : the starting title margin in pixels |
See also:
setTitleMarginTop
public void setTitleMarginTop (int margin)
Sets the top title margin in pixels.
Related XML Attributes:
Parameters | |
---|---|
margin |
int : the top title margin in pixels |
See also:
setTitleTextAppearance
public void setTitleTextAppearance (Context context, int resId)
Sets the text color, size, style, hint color, and highlight color from the specified TextAppearance resource.
Parameters | |
---|---|
context |
Context |
resId |
int |
setTitleTextColor
public void setTitleTextColor (int color)
Sets the text color of the title, if present.
Parameters | |
---|---|
color |
int : The new text color in 0xAARRGGBB format |
showOverflowMenu
public boolean showOverflowMenu ()
Show the overflow items from the associated menu.
Returns | |
---|---|
boolean |
true if the menu was able to be shown, false otherwise |
Protected methods
checkLayoutParams
protected boolean checkLayoutParams (ViewGroup.LayoutParams p)
Parameters | |
---|---|
p |
ViewGroup.LayoutParams |
Returns | |
---|---|
boolean |
generateDefaultLayoutParams
protected Toolbar.LayoutParams generateDefaultLayoutParams ()
Returns a set of default layout parameters. These parameters are requested
when the View passed to addView(android.view.View)
has no layout parameters
already set. If null is returned, an exception is thrown from addView.
Returns | |
---|---|
Toolbar.LayoutParams |
a set of default layout parameters or null |
generateLayoutParams
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 |
ViewGroup.LayoutParams : The layout parameters to convert into a suitable set of layout parameters
for this ViewGroup. |
Returns | |
---|---|
Toolbar.LayoutParams |
an instance of ViewGroup.LayoutParams or one
of its descendants |
onAttachedToWindow
protected void onAttachedToWindow ()
This is called when the view is attached to a window. At this point it
has a Surface and will start drawing. Note that this function is
guaranteed to be called before onDraw(android.graphics.Canvas)
,
however it may be called any time before the first onDraw -- including
before or after onMeasure(int, int)
.
If you override this method you must call through to the
superclass implementation.
onDetachedFromWindow
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.
If you override this method you must call through to the
superclass implementation.
onLayout
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 |
boolean : This is a new size or position for this view |
l |
int : Left position, relative to parent |
t |
int : Top position, relative to parent |
r |
int : Right position, relative to parent |
b |
int : Bottom position, relative to parent |
onMeasure
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 overridden 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 |
int : horizontal space requirements as imposed by the parent.
The requirements are encoded with
View.MeasureSpec . |
heightMeasureSpec |
int : vertical space requirements as imposed by the parent.
The requirements are encoded with
View.MeasureSpec . |
onRestoreInstanceState
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.
If you override this method you must call through to the
superclass implementation.
Parameters | |
---|---|
state |
Parcelable : The frozen state that had previously been returned by
onSaveInstanceState() . |
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.
If you override this method you must call through to the
superclass implementation.
Returns | |
---|---|
Parcelable |
Returns a Parcelable object containing the view's current dynamic state, or null if there is nothing interesting to save. |
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2024-12-18 UTC.