Toolbar
class Toolbar : ViewGroup, MenuHost
A standard toolbar for use within application content.
A Toolbar is a generalization of action bars for use within application layouts. While an action bar is traditionally part of an Activity's opaque window decor controlled by the framework, a Toolbar may be placed at any arbitrary level of nesting within a view hierarchy. An application may choose to designate a Toolbar as the action bar for an Activity using the setSupportActionBar() method.
Toolbar supports a more focused feature set than ActionBar. From start to end, a toolbar may contain a combination of the following optional elements:
- A navigation button. This may be an Up arrow, navigation menu toggle, close, collapse, done or another glyph of the app's choosing. This button should always be used to access other navigational destinations within the container of the Toolbar and its signified content or otherwise leave the current context signified by the Toolbar. 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
LayoutParamsindicates aGravityvalue ofCENTER_HORIZONTALthe view will attempt to center within the available space remaining in the Toolbar after all other elements have been measured. - An
action menu. The menu of actions will pin to the end of the Toolbar offering a few frequent, important or typical actions along with an optional overflow menu for additional actions. Action buttons are vertically aligned within the Toolbar's minimum height, if set.
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.
buttonGravitycollapseContentDescriptioncollapseIconcontentInsetEndcontentInsetLeftcontentInsetRightcontentInsetStartcontentInsetStartWithNavigationcontentInsetEndWithActionsgravitylogologoDescriptionmaxButtonHeightnavigationContentDescriptionnavigationIconpopupThemesubtitlesubtitleTextAppearancesubtitleTextColortitletitleMargintitleMarginBottomtitleMarginEndtitleMarginStarttitleMarginToptitleTextAppearancetitleTextColormenu
Summary
Nested types |
|---|
|
Layout information for child views of Toolbars. |
interface Toolbar.OnMenuItemClickListenerInterface responsible for receiving menu item click events if the items themselves do not have individual item click listeners. |
class Toolbar.SavedState : AbsSavedState |
Public constructors |
|---|
Toolbar(context: Context, attrs: AttributeSet?) |
Toolbar(context: Context, attrs: AttributeSet?, defStyleAttr: Int) |
Public functions |
|
|---|---|
Unit |
@MainThreadAdds the given |
Unit |
@MainThreadAdds the given |
Unit |
@MainThreadAdds the given |
Unit |
Collapse a currently expanded action view. |
Unit |
Dismiss all currently showing popup menus, including overflow or submenus. |
Toolbar.LayoutParams! |
generateLayoutParams(attrs: AttributeSet!) |
CharSequence? |
@Attribute(value = "androidx.appcompat:collapseContentDescription")Retrieve the currently configured content description for the collapse button view. |
Drawable? |
@Attribute(value = "androidx.appcompat:collapseIcon")Return the current drawable used as the collapse icon. |
Int |
@Attribute(value = "androidx.appcompat:contentInsetEnd")Gets the ending content inset for this toolbar. |
Int |
@Attribute(value = "androidx.appcompat:contentInsetEndWithActions")Gets the end content inset to use when action buttons are present. |
Int |
@Attribute(value = "androidx.appcompat:contentInsetLeft")Gets the left content inset for this toolbar. |
Int |
@Attribute(value = "androidx.appcompat:contentInsetRight")Gets the right content inset for this toolbar. |
Int |
@Attribute(value = "androidx.appcompat:contentInsetStart")Gets the starting content inset for this toolbar. |
Int |
@Attribute(value = "androidx.appcompat:contentInsetStartWithNavigation")Gets the start content inset to use when a navigation button is present. |
Int |
Gets the content inset that will be used on the ending side of the bar in the current toolbar configuration. |
Int |
Gets the content inset that will be used on the left side of the bar in the current toolbar configuration. |
Int |
Gets the content inset that will be used on the right side of the bar in the current toolbar configuration. |
Int |
Gets the content inset that will be used on the starting side of the bar in the current toolbar configuration. |
Drawable! |
Return the current logo drawable. |
CharSequence! |
@Attribute(value = "androidx.appcompat:logoDescription")Return the description of the toolbar's logo. |
Menu! |
Return the Menu shown in the toolbar. |
CharSequence? |
@Attribute(value = "androidx.appcompat:navigationContentDescription")Retrieve the currently configured content description for the navigation button view. |
Drawable? |
@Attribute(value = "androidx.appcompat:navigationIcon")Return the current drawable used as the navigation icon. |
Drawable? |
Return the current drawable used as the overflow icon. |
@StyleRes Int |
@Attribute(value = "androidx.appcompat:popupTheme") |
CharSequence! |
@Attribute(value = "androidx.appcompat:subtitle")Return the subtitle of this toolbar. |
CharSequence! |
Returns the title of this toolbar. |
Int |
@Attribute(value = "androidx.appcompat:titleMarginBottom") |
Int |
@Attribute(value = "androidx.appcompat:titleMarginEnd") |
Int |
@Attribute(value = "androidx.appcompat:titleMarginStart") |
Int |
@Attribute(value = "androidx.appcompat:titleMarginTop") |
Boolean |
Check whether this Toolbar is currently hosting an expanded action view. |
Boolean |
Hide the overflow items from the associated menu. |
Unit |
inflateMenu(resId: @MenuRes Int)Inflate a menu resource into this toolbar. |
Unit |
Invalidates the to ensure that what is displayed matches the current internal state of the menu. |
Boolean |
Returns whether the toolbar will attempt to register its own |
Boolean |
Check whether the overflow menu is currently showing. |
Boolean |
onHoverEvent(ev: MotionEvent!) |
Unit |
onRtlPropertiesChanged(layoutDirection: Int) |
Boolean |
onTouchEvent(ev: MotionEvent!) |
Unit |
@MainThreadRemoves the given |
Unit |
setBackInvokedCallbackEnabled(enabled: Boolean)Sets whether the toolbar will attempt to register its own |
Unit |
setCollapseContentDescription(description: CharSequence?)Set a content description for the collapse button if one is present. |
Unit |
setCollapseContentDescription(resId: @StringRes Int)Set a content description for the collapse button if one is present. |
Unit |
setCollapseIcon(icon: Drawable?)Set the icon to use for the toolbar's collapse button. |
Unit |
setCollapseIcon(resId: @DrawableRes Int)Set the icon to use for the toolbar's collapse button. |
Unit |
setContentInsetEndWithActions(insetEndWithActions: Int)Sets the start content inset to use when action buttons are present. |
Unit |
setContentInsetStartWithNavigation(insetStartWithNavigation: Int)Sets the start content inset to use when a navigation button is present. |
Unit |
setContentInsetsAbsolute(contentInsetLeft: Int, contentInsetRight: Int)Sets the content insets for this toolbar. |
Unit |
setContentInsetsRelative(contentInsetStart: Int, contentInsetEnd: Int)Sets the content insets for this toolbar relative to layout direction. |
Unit |
Set a logo drawable. |
Unit |
setLogo(resId: @DrawableRes Int)Set a logo drawable from a resource id. |
Unit |
setLogoDescription(description: CharSequence!)Set a description of the toolbar's logo. |
Unit |
setLogoDescription(resId: @StringRes Int)Set a description of the toolbar's logo. |
Unit |
setNavigationContentDescription(description: CharSequence?)Set a content description for the navigation button if one is present. |
Unit |
setNavigationContentDescription(resId: @StringRes Int)Set a content description for the navigation button if one is present. |
Unit |
setNavigationIcon(icon: Drawable?)Set the icon to use for the toolbar's navigation button. |
Unit |
setNavigationIcon(resId: @DrawableRes Int)Set the icon to use for the toolbar's navigation button. |
Unit |
setNavigationOnClickListener(listener: View.OnClickListener!)Set a listener to respond to navigation events. |
Unit |
Set a listener to respond to menu item click events. |
Unit |
setOverflowIcon(icon: Drawable?)Set the icon to use for the overflow button. |
Unit |
setPopupTheme(resId: @StyleRes Int)Specifies the theme to use when inflating popup menus. |
Unit |
setSubtitle(resId: @StringRes Int)Set the subtitle of this toolbar. |
Unit |
setSubtitle(subtitle: CharSequence!)Set the subtitle of this toolbar. |
Unit |
setSubtitleTextAppearance(context: Context!, resId: @StyleRes Int)Sets the text color, size, style, hint color, and highlight color from the specified TextAppearance resource. |
Unit |
setSubtitleTextColor(color: ColorStateList)Sets the text color of the subtitle, if present. |
Unit |
setSubtitleTextColor(color: @ColorInt Int)Sets the text color of the subtitle, if present. |
Unit |
Set the title of this toolbar. |
Unit |
setTitle(title: CharSequence!)Set the title of this toolbar. |
Unit |
setTitleMargin(start: Int, top: Int, end: Int, bottom: Int)Sets the title margin. |
Unit |
setTitleMarginBottom(margin: Int)Sets the bottom title margin in pixels. |
Unit |
setTitleMarginEnd(margin: Int)Sets the ending title margin in pixels. |
Unit |
setTitleMarginStart(margin: Int)Sets the starting title margin in pixels. |
Unit |
setTitleMarginTop(margin: Int)Sets the top title margin in pixels. |
Unit |
setTitleTextAppearance(context: Context!, resId: @StyleRes Int)Sets the text color, size, style, hint color, and highlight color from the specified TextAppearance resource. |
Unit |
setTitleTextColor(color: ColorStateList)Sets the text color of the title, if present. |
Unit |
setTitleTextColor(color: @ColorInt Int)Sets the text color of the title, if present. |
Boolean |
Show the overflow items from the associated menu. |
Protected functions |
|
|---|---|
Boolean |
|
Toolbar.LayoutParams! |
|
Toolbar.LayoutParams! |
|
Unit |
|
Unit |
|
Unit |
|
Unit |
|
Unit |
onRestoreInstanceState(state: Parcelable!) |
Parcelable! |
Extension functions |
|
|---|---|
Unit |
Toolbar.setupWithNavController(Sets up a |
Unit |
Toolbar.setupWithNavController(Sets up a |
Inherited Constants |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Public constructors
Public functions
addMenuProvider
@MainThread
fun addMenuProvider(provider: MenuProvider): Unit
Adds the given MenuProvider to this MenuHost. If using this method, you must manually remove the provider when necessary.
| Parameters | |
|---|---|
provider: MenuProvider |
the MenuProvider to be added |
| See also | |
|---|---|
removeMenuProvider |
addMenuProvider
@MainThread
fun addMenuProvider(provider: MenuProvider, owner: LifecycleOwner): Unit
Adds the given MenuProvider to this MenuHost. This MenuProvider will be removed once the given LifecycleOwner receives an Lifecycle.Event.ON_DESTROY event.
| Parameters | |
|---|---|
provider: MenuProvider |
the MenuProvider to be added |
owner: LifecycleOwner |
the Lifecycle owner whose state will determine the removal of the provider |
addMenuProvider
@MainThread
fun addMenuProvider(
provider: MenuProvider,
owner: LifecycleOwner,
state: Lifecycle.State
): Unit
Adds the given MenuProvider to this MenuHost once the given LifecycleOwner reaches the given Lifecycle.State. This MenuProvider will be removed once the given LifecycleOwner goes down from the given Lifecycle.State.
| Parameters | |
|---|---|
provider: MenuProvider |
the MenuProvider to be added |
owner: LifecycleOwner |
the Lifecycle owner whose state will be used for automated addition/removal |
state: Lifecycle.State |
the Lifecycle.State to check for automated addition/removal |
collapseActionView
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.
| See also | |
|---|---|
hasExpandedActionView |
dismissPopupMenus
fun dismissPopupMenus(): Unit
Dismiss all currently showing popup menus, including overflow or submenus.
generateLayoutParams
fun generateLayoutParams(attrs: AttributeSet!): Toolbar.LayoutParams!
getCollapseContentDescription
@Attribute(value = "androidx.appcompat:collapseContentDescription")
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.
| Returns | |
|---|---|
CharSequence? |
The collapse button's content description |
getCollapseIcon
@Attribute(value = "androidx.appcompat:collapseIcon")
fun getCollapseIcon(): Drawable?
Return the current drawable used as the collapse icon.
| Returns | |
|---|---|
Drawable? |
The collapse icon drawable |
getContentInsetEnd
@Attribute(value = "androidx.appcompat:contentInsetEnd")
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.
| Returns | |
|---|---|
Int |
The ending content inset for this toolbar |
getContentInsetEndWithActions
@Attribute(value = "androidx.appcompat:contentInsetEndWithActions")
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.
| Returns | |
|---|---|
Int |
the end content inset used when a menu has been set in pixels |
getContentInsetLeft
@Attribute(value = "androidx.appcompat:contentInsetLeft")
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.
| Returns | |
|---|---|
Int |
The left content inset for this toolbar |
getContentInsetRight
@Attribute(value = "androidx.appcompat:contentInsetRight")
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.
| Returns | |
|---|---|
Int |
The right content inset for this toolbar |
getContentInsetStart
@Attribute(value = "androidx.appcompat:contentInsetStart")
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.
| Returns | |
|---|---|
Int |
The starting content inset for this toolbar |
getContentInsetStartWithNavigation
@Attribute(value = "androidx.appcompat:contentInsetStartWithNavigation")
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.
| Returns | |
|---|---|
Int |
the start content inset used when a navigation icon has been set in pixels |
getCurrentContentInsetEnd
fun getCurrentContentInsetEnd(): Int
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 | |
|---|---|
getContentInsetEndWithActions |
getCurrentContentInsetLeft
fun getCurrentContentInsetLeft(): Int
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
fun getCurrentContentInsetRight(): Int
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
fun getCurrentContentInsetStart(): Int
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 | |
|---|---|
getContentInsetStartWithNavigation |
getLogo
@Attribute(value = "androidx.appcompat:logo")
fun getLogo(): Drawable!
Return the current logo drawable.
| Returns | |
|---|---|
Drawable! |
The current logo drawable |
getLogoDescription
@Attribute(value = "androidx.appcompat:logoDescription")
fun getLogoDescription(): CharSequence!
Return the description of the toolbar's logo.
| Returns | |
|---|---|
CharSequence! |
A description of the logo |
getMenu
@Attribute(value = "androidx.appcompat:menu")
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.
| Returns | |
|---|---|
Menu! |
The toolbar's Menu menu |
getNavigationContentDescription
@Attribute(value = "androidx.appcompat:navigationContentDescription")
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.
| Returns | |
|---|---|
CharSequence? |
The navigation button's content description |
getNavigationIcon
@Attribute(value = "androidx.appcompat:navigationIcon")
fun getNavigationIcon(): Drawable?
Return the current drawable used as the navigation icon.
| Returns | |
|---|---|
Drawable? |
The navigation icon drawable |
getOverflowIcon
fun getOverflowIcon(): Drawable?
Return the current drawable used as the overflow icon.
| Returns | |
|---|---|
Drawable? |
The overflow icon drawable |
getPopupTheme
@Attribute(value = "androidx.appcompat:popupTheme")
fun getPopupTheme(): @StyleRes Int
| Returns | |
|---|---|
@StyleRes Int |
resource identifier of the theme used to inflate popup menus, or 0 if menus are inflated against the toolbar theme |
| See also | |
|---|---|
setPopupTheme |
getSubtitle
@Attribute(value = "androidx.appcompat:subtitle")
fun getSubtitle(): CharSequence!
Return the subtitle of this toolbar.
| Returns | |
|---|---|
CharSequence! |
The current subtitle |
getTitle
@Attribute(value = "androidx.appcompat:title")
fun getTitle(): CharSequence!
Returns the title of this toolbar.
| Returns | |
|---|---|
CharSequence! |
The current title. |
getTitleMarginBottom
@Attribute(value = "androidx.appcompat:titleMarginBottom")
fun getTitleMarginBottom(): Int
| Returns | |
|---|---|
Int |
the bottom title margin in pixels |
| See also | |
|---|---|
setTitleMarginBottom |
getTitleMarginEnd
@Attribute(value = "androidx.appcompat:titleMarginEnd")
fun getTitleMarginEnd(): Int
| Returns | |
|---|---|
Int |
the ending title margin in pixels |
| See also | |
|---|---|
setTitleMarginEnd |
getTitleMarginStart
@Attribute(value = "androidx.appcompat:titleMarginStart")
fun getTitleMarginStart(): Int
| Returns | |
|---|---|
Int |
the starting title margin in pixels |
| See also | |
|---|---|
setTitleMarginStart |
getTitleMarginTop
@Attribute(value = "androidx.appcompat:titleMarginTop")
fun getTitleMarginTop(): Int
| Returns | |
|---|---|
Int |
the top title margin in pixels |
| See also | |
|---|---|
setTitleMarginTop |
hasExpandedActionView
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.
| Returns | |
|---|---|
Boolean |
true if the Toolbar has an expanded action view |
hideOverflowMenu
fun hideOverflowMenu(): Boolean
Hide the overflow items from the associated menu.
| Returns | |
|---|---|
Boolean |
true if the menu was able to be hidden, false otherwise |
inflateMenu
fun inflateMenu(resId: @MenuRes 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.
invalidateMenu
@MainThread
fun invalidateMenu(): Unit
Invalidates the to ensure that what is displayed matches the current internal state of the menu. This should be called whenever the state of the menu is changed, such as items being removed or disabled based on some user event. Only the items in the Menu that were provided by MenuProviders should be removed and repopulated, leaving all manually inflated menu items untouched, as they should continue to be managed manually.
isBackInvokedCallbackEnabled
fun isBackInvokedCallbackEnabled(): Boolean
Returns whether the toolbar will attempt to register its own OnBackInvokedCallback in supported configurations to handle collapsing expanded action items when a back invocation occurs.
| See also | |
|---|---|
setBackInvokedCallbackEnabled |
isOverflowMenuShowing
fun isOverflowMenuShowing(): Boolean
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 |
removeMenuProvider
@MainThread
fun removeMenuProvider(provider: MenuProvider): Unit
Removes the given MenuProvider from this MenuHost.
| Parameters | |
|---|---|
provider: MenuProvider |
the MenuProvider to be removed |
setBackInvokedCallbackEnabled
fun setBackInvokedCallbackEnabled(enabled: Boolean): Unit
Sets whether the toolbar will attempt to register its own OnBackInvokedCallback in supported configurations to handle collapsing expanded action items when a back invocation occurs.
This feature is only supported on SDK 33 and above for applications that have enabled back invocation callback handling.
| Parameters | |
|---|---|
enabled: Boolean |
|
| See also | |
|---|---|
isBackInvokedCallbackEnabled |
setCollapseContentDescription
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 |
setCollapseContentDescription
fun setCollapseContentDescription(resId: @StringRes 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: @StringRes Int |
Resource ID of a content description string to set, or 0 to clear the description |
setCollapseIcon
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
fun setCollapseIcon(resId: @DrawableRes 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: @DrawableRes Int |
Resource ID of a drawable to set |
setContentInsetEndWithActions
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
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
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.
setContentInsetsRelative
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.
setLogo
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.
| Parameters | |
|---|---|
drawable: Drawable! |
Drawable to use as a logo |
setLogo
fun setLogo(resId: @DrawableRes 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.
| Parameters | |
|---|---|
resId: @DrawableRes Int |
ID of a drawable resource |
setLogoDescription
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 |
setLogoDescription
fun setLogoDescription(resId: @StringRes Int): Unit
Set a description of the toolbar's logo.
This description will be used for accessibility or other similar descriptions of the UI.
setNavigationContentDescription
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 |
setNavigationContentDescription
fun setNavigationContentDescription(resId: @StringRes 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: @StringRes Int |
Resource ID of a content description string to set, or 0 to clear the description |
setNavigationIcon
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. This is used for accessibility and tooltips.
| Parameters | |
|---|---|
icon: Drawable? |
Drawable to set, may be null to clear the icon |
setNavigationIcon
fun setNavigationIcon(resId: @DrawableRes 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. This is used for accessibility and tooltips.
| Parameters | |
|---|---|
resId: @DrawableRes Int |
Resource ID of a drawable to set |
setNavigationOnClickListener
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 |
| See also | |
|---|---|
setNavigationIcon |
setOnMenuItemClickListener
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
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
fun setPopupTheme(resId: @StyleRes Int): Unit
Specifies the theme to use when inflating popup menus. By default, uses the same theme as the toolbar itself.
| See also | |
|---|---|
getPopupTheme |
setSubtitle
fun setSubtitle(resId: @StringRes Int): Unit
Set the subtitle of this toolbar.
Subtitles should express extended information about the current content.
setSubtitle
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
fun setSubtitleTextAppearance(context: Context!, resId: @StyleRes Int): Unit
Sets the text color, size, style, hint color, and highlight color from the specified TextAppearance resource.
setSubtitleTextColor
fun setSubtitleTextColor(color: ColorStateList): Unit
Sets the text color of the subtitle, if present.
| Parameters | |
|---|---|
color: ColorStateList |
The new text color |
setSubtitleTextColor
fun setSubtitleTextColor(color: @ColorInt Int): Unit
Sets the text color of the subtitle, if present.
setTitle
fun setTitle(resId: @StringRes 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.
setTitle
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
fun setTitleMargin(start: Int, top: Int, end: Int, bottom: Int): Unit
Sets the title margin.
setTitleMarginBottom
fun setTitleMarginBottom(margin: Int): Unit
Sets the bottom title margin in pixels.
| Parameters | |
|---|---|
margin: Int |
the bottom title margin in pixels |
| See also | |
|---|---|
getTitleMarginBottom |
setTitleMarginEnd
fun setTitleMarginEnd(margin: Int): Unit
Sets the ending title margin in pixels.
| Parameters | |
|---|---|
margin: Int |
the ending title margin in pixels |
| See also | |
|---|---|
getTitleMarginEnd |
setTitleMarginStart
fun setTitleMarginStart(margin: Int): Unit
Sets the starting title margin in pixels.
| Parameters | |
|---|---|
margin: Int |
the starting title margin in pixels |
| See also | |
|---|---|
getTitleMarginStart |
setTitleMarginTop
fun setTitleMarginTop(margin: Int): Unit
Sets the top title margin in pixels.
| Parameters | |
|---|---|
margin: Int |
the top title margin in pixels |
| See also | |
|---|---|
getTitleMarginTop |
setTitleTextAppearance
fun setTitleTextAppearance(context: Context!, resId: @StyleRes Int): Unit
Sets the text color, size, style, hint color, and highlight color from the specified TextAppearance resource.
setTitleTextColor
fun setTitleTextColor(color: ColorStateList): Unit
Sets the text color of the title, if present.
| Parameters | |
|---|---|
color: ColorStateList |
The new text color |
setTitleTextColor
fun setTitleTextColor(color: @ColorInt Int): Unit
Sets the text color of the title, if present.
showOverflowMenu
fun showOverflowMenu(): Boolean
Show the overflow items from the associated menu.
| Returns | |
|---|---|
Boolean |
true if the menu was able to be shown, false otherwise |
Protected functions
generateDefaultLayoutParams
protected fun generateDefaultLayoutParams(): Toolbar.LayoutParams!
generateLayoutParams
protected fun generateLayoutParams(p: ViewGroup.LayoutParams!): Toolbar.LayoutParams!
Extension functions
Toolbar.setupWithNavController
fun Toolbar.setupWithNavController(
navController: NavController,
configuration: AppBarConfiguration = AppBarConfiguration(navController.graph)
): Unit
Sets up a Toolbar for use with a NavController.
By calling this method, the title in the Toolbar will automatically be updated when the destination changes (assuming there is a valid label).
The AppBarConfiguration you provide controls how the Navigation button is displayed and what action is triggered when the Navigation button is tapped.
This method will call NavController.navigateUp when the navigation icon is clicked.
| Parameters | |
|---|---|
navController: NavController |
The NavController whose navigation actions will be reflected in the title of the Toolbar. |
configuration: AppBarConfiguration = AppBarConfiguration(navController.graph) |
Additional configuration options for customizing the behavior of the Toolbar |
Toolbar.setupWithNavController
fun Toolbar.setupWithNavController(
navController: NavController,
drawerLayout: DrawerLayout?
): Unit
Sets up a Toolbar for use with a NavController.
By calling this method, the title in the Toolbar will automatically be updated when the destination changes (assuming there is a valid label).
The start destination of your navigation graph is considered the only top level destination. On the start destination of your navigation graph, the Toolbar will show the drawer icon if the given drawerLayout is non null. On all other destinations, the Toolbar will show the Up button.
This method will call NavController.navigateUp when the navigation icon is clicked.
| Parameters | |
|---|---|
navController: NavController |
The NavController whose navigation actions will be reflected in the title of the Toolbar. |
drawerLayout: DrawerLayout? |
The DrawerLayout that should be toggled from the Navigation button |