Added in API level 21

Toolbar


open class Toolbar : ViewGroup
kotlin.Any
   ↳ 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 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. Action buttons are vertically aligned within the Toolbar's minimum 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
open

Layout information for child views of Toolbars.

abstract

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
Public constructors
Toolbar(context: Context!)

Toolbar(context: Context!, attrs: AttributeSet!)

Toolbar(context: Context!, attrs: AttributeSet!, defStyleAttr: Int)

Toolbar(context: Context!, attrs: AttributeSet!, defStyleAttr: Int, defStyleRes: Int)

Public methods
open Unit

Collapse a currently expanded action view.

open Unit

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

open Toolbar.LayoutParams!

open CharSequence?

Retrieve the currently configured content description for the collapse button view.

open Drawable?

Return the current drawable used as the collapse icon.

open Int

Gets the ending content inset for this toolbar.

open Int

Gets the end content inset to use when action buttons are present.

open Int

Gets the left content inset for this toolbar.

open Int

Gets the right content inset for this toolbar.

open Int

Gets the starting content inset for this toolbar.

open Int

Gets the start content inset to use when a navigation button is present.

open Int

Gets the content inset that will be used on the ending side of the bar in the current toolbar configuration.

open Int

Gets the content inset that will be used on the left side of the bar in the current toolbar configuration.

open Int

Gets the content inset that will be used on the right side of the bar in the current toolbar configuration.

open Int

Gets the content inset that will be used on the starting side of the bar in the current toolbar configuration.

open Drawable!

Return the current logo drawable.

open CharSequence!

Return the description of the toolbar's logo.

open Menu!

Return the Menu shown in the toolbar.

open CharSequence?

Retrieve the currently configured content description for the navigation button view.

open Drawable?

Return the current drawable used as the navigation icon.

open Drawable?

Return the current drawable used as the overflow icon.

open Int

open CharSequence!

Return the subtitle of this toolbar.

open CharSequence!

Returns the title of this toolbar.

open Int

open Int

open Int

open Int

open Boolean

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

open Boolean

Hide the overflow items from the associated menu.

open Unit
inflateMenu(resId: Int)

Inflate a menu resource into this toolbar.

open Boolean

Check whether the overflow menu is currently showing.

open Unit
onRtlPropertiesChanged(layoutDirection: Int)

Called when any RTL property (layout direction or text direction or text alignment) has been changed.

open Boolean

open Unit

Set a content description for the collapse button if one is present.

open Unit

Set a content description for the collapse button if one is present.

open Unit

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

open Unit

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

open Unit
setContentInsetEndWithActions(insetEndWithActions: Int)

Sets the start content inset to use when action buttons are present.

open Unit
setContentInsetStartWithNavigation(insetStartWithNavigation: Int)

Sets the start content inset to use when a navigation button is present.

open Unit
setContentInsetsAbsolute(contentInsetLeft: Int, contentInsetRight: Int)

Sets the content insets for this toolbar.

open Unit
setContentInsetsRelative(contentInsetStart: Int, contentInsetEnd: Int)

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

open Unit
setLogo(drawable: Drawable!)

Set a logo drawable.

open Unit
setLogo(resId: Int)

Set a logo drawable from a resource id.

open Unit

Set a description of the toolbar's logo.

open Unit

Set a description of the toolbar's logo.

open Unit

Set a content description for the navigation button if one is present.

open Unit

Set a content description for the navigation button if one is present.

open Unit

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

open Unit

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

open Unit

Set a listener to respond to navigation events.

open Unit

Set a listener to respond to menu item click events.

open Unit

Set the icon to use for the overflow button.

open Unit

Specifies the theme to use when inflating popup menus.

open Unit
setSubtitle(resId: Int)

Set the subtitle of this toolbar.

open Unit

Set the subtitle of this toolbar.

open Unit

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

open Unit

Sets the text color of the subtitle, if present.

open Unit
setTitle(resId: Int)

Set the title of this toolbar.

open Unit

Set the title of this toolbar.

open Unit
setTitleMargin(start: Int, top: Int, end: Int, bottom: Int)

Sets the title margin.

open Unit

Sets the bottom title margin in pixels.

open Unit

Sets the ending title margin in pixels.

open Unit

Sets the starting title margin in pixels.

open Unit

Sets the top title margin in pixels.

open Unit
setTitleTextAppearance(context: Context!, resId: Int)

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

open Unit

Sets the text color of the title, if present.

open Boolean

Show the overflow items from the associated menu.

Protected methods
open Boolean

open Toolbar.LayoutParams!

open Toolbar.LayoutParams!

open Unit

open Unit

open Unit
onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int)

open Unit
onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int)

open Unit

open Parcelable?

Inherited functions
Inherited properties

XML attributes

android:buttonGravity

android:buttonGravity

android:collapseContentDescription

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;

android:collapseIcon

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".

android:contentInsetEnd

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).

android:contentInsetEndWithActions

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).

android:contentInsetLeft

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).

android:contentInsetRight

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).

android:contentInsetStart

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).

android:contentInsetStartWithNavigation

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).

android:gravity

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

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:maxButtonHeight

android:navigationContentDescription

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;

android:navigationIcon

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".

android:popupTheme

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

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:subtitleTextAppearance

android:subtitleTextColor

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

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

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).

android:titleMarginBottom

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).

android:titleMarginEnd

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).

android:titleMarginStart

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).

android:titleMarginTop

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).

android:titleTextAppearance

android:titleTextAppearance

android:titleTextColor

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

Added in API level 21
Toolbar(context: Context!)

Toolbar

Added in API level 21
Toolbar(
    context: Context!,
    attrs: AttributeSet!)

Toolbar

Added in API level 21
Toolbar(
    context: Context!,
    attrs: AttributeSet!,
    defStyleAttr: Int)

Toolbar

Added in API level 21
Toolbar(
    context: Context!,
    attrs: AttributeSet!,
    defStyleAttr: Int,
    defStyleRes: Int)

Public methods

collapseActionView

Added in API level 21
open fun collapseActionView(): Unit

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.

dismissPopupMenus

Added in API level 21
open fun dismissPopupMenus(): Unit

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

generateLayoutParams

Added in API level 21
open fun generateLayoutParams(attrs: AttributeSet!): Toolbar.LayoutParams!
Parameters
attrs AttributeSet!: the attributes to build the layout parameters from
Return
Toolbar.LayoutParams! an instance of android.view.ViewGroup.LayoutParams or one of its descendants

getCollapseContentDescription

Added in API level 29
open fun getCollapseContentDescription(): CharSequence?

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.

Return
CharSequence? The collapse button's content description This value may be null.

getCollapseIcon

Added in API level 29
open fun getCollapseIcon(): Drawable?

Return the current drawable used as the collapse icon.

Return
Drawable? The collapse icon drawable This value may be null.

getContentInsetEnd

Added in API level 21
open fun getContentInsetEnd(): Int

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.

Return
Int The ending content inset for this toolbar

getContentInsetEndWithActions

Added in API level 24
open fun getContentInsetEndWithActions(): Int

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.

Return
Int the end content inset used when a menu has been set in pixels

getContentInsetLeft

Added in API level 21
open fun getContentInsetLeft(): Int

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.

Return
Int The left content inset for this toolbar

getContentInsetRight

Added in API level 21
open fun getContentInsetRight(): Int

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.

Return
Int The right content inset for this toolbar

getContentInsetStart

Added in API level 21
open fun getContentInsetStart(): Int

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.

Return
Int The starting content inset for this toolbar

getContentInsetStartWithNavigation

Added in API level 24
open fun getContentInsetStartWithNavigation(): Int

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.

Return
Int the start content inset used when a navigation icon has been set in pixels

getCurrentContentInsetEnd

Added in API level 24
open fun getCurrentContentInsetEnd(): Int

Gets the content inset that will be used on the ending side of the bar in the current toolbar configuration.

Return
Int the current content inset end in pixels

getCurrentContentInsetLeft

Added in API level 24
open fun getCurrentContentInsetLeft(): Int

Gets the content inset that will be used on the left side of the bar in the current toolbar configuration.

Return
Int the current content inset left in pixels

getCurrentContentInsetRight

Added in API level 24
open fun getCurrentContentInsetRight(): Int

Gets the content inset that will be used on the right side of the bar in the current toolbar configuration.

Return
Int the current content inset right in pixels

getCurrentContentInsetStart

Added in API level 24
open fun getCurrentContentInsetStart(): Int

Gets the content inset that will be used on the starting side of the bar in the current toolbar configuration.

Return
Int the current content inset start in pixels
Added in API level 21
open fun getLogo(): Drawable!

Return the current logo drawable.

Return
Drawable! The current logo drawable

getLogoDescription

Added in API level 21
open fun getLogoDescription(): CharSequence!

Return the description of the toolbar's logo.

Return
CharSequence! A description of the logo

getMenu

Added in API level 21
open fun getMenu(): Menu!

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).

Return
Menu! The toolbar's Menu

getNavigationContentDescription

Added in API level 21
open fun getNavigationContentDescription(): CharSequence?

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.

Return
CharSequence? The navigation button's content description This value may be null.

getNavigationIcon

Added in API level 21
open fun getNavigationIcon(): Drawable?

Return the current drawable used as the navigation icon.

Return
Drawable? The navigation icon drawable This value may be null.

getOverflowIcon

Added in API level 23
open fun getOverflowIcon(): Drawable?

Return the current drawable used as the overflow icon.

Return
Drawable? The overflow icon drawable This value may be null.

getPopupTheme

Added in API level 21
open fun getPopupTheme(): Int
Return
Int resource identifier of the theme used to inflate popup menus, or 0 if menus are inflated against the toolbar theme

getSubtitle

Added in API level 21
open fun getSubtitle(): CharSequence!

Return the subtitle of this toolbar.

Return
CharSequence! The current subtitle

getTitle

Added in API level 21
open fun getTitle(): CharSequence!

Returns the title of this toolbar.

Return
CharSequence! The current title.

getTitleMarginBottom

Added in API level 24
open fun getTitleMarginBottom(): Int
Return
Int the bottom title margin in pixels

getTitleMarginEnd

Added in API level 24
open fun getTitleMarginEnd(): Int
Return
Int the ending title margin in pixels

getTitleMarginStart

Added in API level 24
open fun getTitleMarginStart(): Int
Return
Int the starting title margin in pixels

getTitleMarginTop

Added in API level 24
open fun getTitleMarginTop(): Int
Return
Int the top title margin in pixels

hasExpandedActionView

Added in API level 21
open fun hasExpandedActionView(): Boolean

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.

Return
Boolean true if the Toolbar has an expanded action view

hideOverflowMenu

Added in API level 21
open fun hideOverflowMenu(): Boolean

Hide the overflow items from the associated menu.

Return
Boolean true if the menu was able to be hidden, false otherwise

inflateMenu

Added in API level 21
open fun inflateMenu(resId: Int): Unit

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

Added in API level 21
open fun isOverflowMenuShowing(): Boolean

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

Return
Boolean true if the overflow menu is currently showing

onRtlPropertiesChanged

Added in API level 21
open fun onRtlPropertiesChanged(layoutDirection: Int): Unit

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 android.view.View#LAYOUT_DIRECTION_LTR, or android.view.View#LAYOUT_DIRECTION_RTL

onTouchEvent

Added in API level 21
open fun onTouchEvent(ev: MotionEvent!): Boolean
Parameters
event The motion event.
Return
Boolean True if the event was handled, false otherwise.

setCollapseContentDescription

Added in API level 29
open fun setCollapseContentDescription(resId: Int): Unit

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.

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

setCollapseContentDescription

Added in API level 29
open fun setCollapseContentDescription(description: CharSequence?): Unit

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.

Parameters
description CharSequence?: Content description to set, or null to clear the content description

setCollapseIcon

Added in API level 29
open fun setCollapseIcon(icon: Drawable?): Unit

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 .

Parameters
icon Drawable?: Drawable to set, may be null to use the default icon

setCollapseIcon

Added in API level 29
open fun setCollapseIcon(resId: Int): Unit

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 .

Parameters
resId Int: Resource ID of a drawable to set

setContentInsetEndWithActions

Added in API level 24
open fun setContentInsetEndWithActions(insetEndWithActions: Int): Unit

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.

Parameters
insetEndWithActions Int: the inset to use when a menu has been set in pixels

setContentInsetStartWithNavigation

Added in API level 24
open fun setContentInsetStartWithNavigation(insetStartWithNavigation: Int): Unit

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.

Parameters
insetStartWithNavigation Int: the inset to use when a navigation icon has been set in pixels

setContentInsetsAbsolute

Added in API level 21
open fun setContentInsetsAbsolute(
    contentInsetLeft: Int,
    contentInsetRight: Int
): Unit

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.

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

setContentInsetsRelative

Added in API level 21
open fun setContentInsetsRelative(
    contentInsetStart: Int,
    contentInsetEnd: Int
): Unit

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.

Parameters
contentInsetStart Int: Content inset for the toolbar starting edge
contentInsetEnd Int: Content inset for the toolbar ending edge
Added in API level 21
open fun setLogo(drawable: Drawable!): Unit

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

setLogo

Added in API level 21
open fun setLogo(resId: Int): Unit

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

setLogoDescription

Added in API level 21
open fun setLogoDescription(resId: Int): Unit

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

Added in API level 21
open fun setLogoDescription(description: CharSequence!): Unit

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

Added in API level 21
open fun setNavigationContentDescription(resId: Int): Unit

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 Int: Resource ID of a content description string to set, or 0 to clear the description

setNavigationContentDescription

Added in API level 21
open fun setNavigationContentDescription(description: CharSequence?): Unit

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 CharSequence?: Content description to set, or null to clear the content description

setNavigationIcon

Added in API level 21
open fun setNavigationIcon(icon: Drawable?): Unit

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?: Drawable to set, may be null to clear the icon

setNavigationIcon

Added in API level 21
open fun setNavigationIcon(resId: Int): Unit

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 Int: Resource ID of a drawable to set

setNavigationOnClickListener

Added in API level 21
open fun setNavigationOnClickListener(listener: View.OnClickListener!): Unit

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

Added in API level 21
open fun setOnMenuItemClickListener(listener: Toolbar.OnMenuItemClickListener!): Unit

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

Added in API level 23
open fun setOverflowIcon(icon: Drawable?): Unit

Set the icon to use for the overflow button.

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

setPopupTheme

Added in API level 21
open fun setPopupTheme(resId: Int): Unit

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

Added in API level 21
open fun setSubtitle(resId: Int): Unit

Set the subtitle of this toolbar.

Subtitles should express extended information about the current content.

Parameters
resId Int: String resource ID

setSubtitle

Added in API level 21
open fun setSubtitle(subtitle: CharSequence!): Unit

Set the subtitle of this toolbar.

Subtitles should express extended information about the current content.

Parameters
subtitle CharSequence!: Subtitle to set

setSubtitleTextAppearance

Added in API level 21
open fun setSubtitleTextAppearance(
    context: Context!,
    resId: Int
): Unit

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

setSubtitleTextColor

Added in API level 21
open fun setSubtitleTextColor(color: Int): Unit

Sets the text color of the subtitle, if present.

Parameters
color Int: The new text color in 0xAARRGGBB format

setTitle

Added in API level 21
open fun setTitle(resId: Int): Unit

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

setTitle

Added in API level 21
open fun setTitle(title: CharSequence!): Unit

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

setTitleMargin

Added in API level 24
open fun setTitleMargin(
    start: Int,
    top: Int,
    end: Int,
    bottom: Int
): Unit

Sets the title margin.

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

Added in API level 24
open fun setTitleMarginBottom(margin: Int): Unit

Sets the bottom title margin in pixels.

Parameters
margin Int: the bottom title margin in pixels

setTitleMarginEnd

Added in API level 24
open fun setTitleMarginEnd(margin: Int): Unit

Sets the ending title margin in pixels.

Parameters
margin Int: the ending title margin in pixels

setTitleMarginStart

Added in API level 24
open fun setTitleMarginStart(margin: Int): Unit

Sets the starting title margin in pixels.

Parameters
margin Int: the starting title margin in pixels

setTitleMarginTop

Added in API level 24
open fun setTitleMarginTop(margin: Int): Unit

Sets the top title margin in pixels.

Parameters
margin Int: the top title margin in pixels

setTitleTextAppearance

Added in API level 21
open fun setTitleTextAppearance(
    context: Context!,
    resId: Int
): Unit

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

setTitleTextColor

Added in API level 21
open fun setTitleTextColor(color: Int): Unit

Sets the text color of the title, if present.

Parameters
color Int: The new text color in 0xAARRGGBB format

showOverflowMenu

Added in API level 21
open fun showOverflowMenu(): Boolean

Show the overflow items from the associated menu.

Return
Boolean true if the menu was able to be shown, false otherwise

Protected methods

checkLayoutParams

Added in API level 21
protected open fun checkLayoutParams(p: ViewGroup.LayoutParams!): Boolean

generateDefaultLayoutParams

Added in API level 21
protected open fun generateDefaultLayoutParams(): Toolbar.LayoutParams!
Return
Toolbar.LayoutParams! a set of default layout parameters or null

generateLayoutParams

Added in API level 21
protected open fun generateLayoutParams(p: ViewGroup.LayoutParams!): Toolbar.LayoutParams!
Parameters
p ViewGroup.LayoutParams!: The layout parameters to convert into a suitable set of layout parameters for this ViewGroup.
Return
Toolbar.LayoutParams! an instance of android.view.ViewGroup.LayoutParams or one of its descendants

onAttachedToWindow

Added in API level 21
protected open fun onAttachedToWindow(): Unit

onDetachedFromWindow

Added in API level 21
protected open fun onDetachedFromWindow(): Unit

onLayout

Added in API level 21
protected open fun onLayout(
    changed: Boolean,
    l: Int,
    t: Int,
    r: Int,
    b: Int
): Unit
Parameters
changed Boolean: This is a new size or position for this view
left Left position, relative to parent
top Top position, relative to parent
right Right position, relative to parent
bottom Bottom position, relative to parent

onMeasure

Added in API level 21
protected open fun onMeasure(
    widthMeasureSpec: Int,
    heightMeasureSpec: Int
): Unit
Parameters
widthMeasureSpec Int: horizontal space requirements as imposed by the parent. The requirements are encoded with android.view.View.MeasureSpec.
heightMeasureSpec Int: vertical space requirements as imposed by the parent. The requirements are encoded with android.view.View.MeasureSpec.

onRestoreInstanceState

Added in API level 21
protected open fun onRestoreInstanceState(state: Parcelable!): Unit
Parameters
state Parcelable!: The frozen state that had previously been returned by onSaveInstanceState.

onSaveInstanceState

Added in API level 21
protected open fun onSaveInstanceState(): Parcelable?
Return
Parcelable? Returns a Parcelable object containing the view's current dynamic state, or null if there is nothing interesting to save.