Google is committed to advancing racial equity for Black communities. See how.

Toolbar

open class Toolbar : ViewGroup
kotlin.Any
   ↳ android.view.View
   ↳ android.view.ViewGroup
   ↳ androidx.appcompat.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 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 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.

androidx.appcompat.R.attr#buttonGravity androidx.appcompat.R.attr#collapseContentDescription androidx.appcompat.R.attr#collapseIcon androidx.appcompat.R.attr#contentInsetEnd androidx.appcompat.R.attr#contentInsetLeft androidx.appcompat.R.attr#contentInsetRight androidx.appcompat.R.attr#contentInsetStart androidx.appcompat.R.attr#contentInsetStartWithNavigation androidx.appcompat.R.attr#contentInsetEndWithActions android.R.attr#gravity androidx.appcompat.R.attr#logo androidx.appcompat.R.attr#logoDescription androidx.appcompat.R.attr#maxButtonHeight androidx.appcompat.R.attr#navigationContentDescription androidx.appcompat.R.attr#navigationIcon androidx.appcompat.R.attr#popupTheme androidx.appcompat.R.attr#subtitle androidx.appcompat.R.attr#subtitleTextAppearance androidx.appcompat.R.attr#subtitleTextColor androidx.appcompat.R.attr#title androidx.appcompat.R.attr#titleMargin androidx.appcompat.R.attr#titleMarginBottom androidx.appcompat.R.attr#titleMarginEnd androidx.appcompat.R.attr#titleMarginStart androidx.appcompat.R.attr#titleMarginTop androidx.appcompat.R.attr#titleTextAppearance androidx.appcompat.R.attr#titleTextColor androidx.appcompat.R.attr#menu

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.

open

Public constructors
<init>(@NonNull context: Context)

<init>(@NonNull context: Context, @Nullable attrs: AttributeSet?)

<init>(@NonNull context: Context, @Nullable attrs: AttributeSet?, defStyleAttr: 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(@MenuRes resId: Int)

Inflate a menu resource into this toolbar.

open Boolean

Check whether the overflow menu is currently showing.

open Boolean

open Unit
onRtlPropertiesChanged(layoutDirection: Int)

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
setCollapseIcon(@DrawableRes resId: Int)

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

open Unit
setCollapseIcon(@Nullable icon: Drawable?)

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(@DrawableRes resId: Int)

Set a logo drawable from a resource id.

open Unit
setLogo(drawable: Drawable!)

Set a logo drawable.

open Unit
setLogoDescription(@StringRes resId: Int)

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
setNavigationIcon(@DrawableRes resId: Int)

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

open Unit
setNavigationIcon(@Nullable icon: Drawable?)

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
setOverflowIcon(@Nullable icon: Drawable?)

Set the icon to use for the overflow button.

open Unit
setPopupTheme(@StyleRes resId: Int)

Specifies the theme to use when inflating popup menus.

open Unit
setSubtitle(@StringRes resId: Int)

Set the subtitle of this toolbar.

open Unit

Set the subtitle of this toolbar.

open Unit
setSubtitleTextAppearance(context: Context!, @StyleRes resId: Int)

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

open Unit
setSubtitleTextColor(@ColorInt color: Int)

Sets the text color of the subtitle, if present.

open Unit

Sets the text color of the subtitle, if present.

open Unit
setTitle(@StringRes 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!, @StyleRes resId: Int)

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

open Unit
setTitleTextColor(@ColorInt color: Int)

Sets the text color of the title, if present.

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
onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int)

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

open Unit

open Parcelable?

Extension functions
From androidx.navigation.ui.androidx.appcompat.widget.Toolbar
Unit
Toolbar.setupWithNavController(navController: NavController, drawerLayout: DrawerLayout?)

Sets up a Toolbar for use with a NavController.

Unit
Toolbar.setupWithNavController(navController: NavController, configuration: AppBarConfiguration = AppBarConfiguration(navController.graph))

Sets up a Toolbar for use with a NavController.

Public constructors

<init>

Toolbar(@NonNull context: Context)

<init>

Toolbar(
    @NonNull context: Context,
    @Nullable attrs: AttributeSet?)

<init>

Toolbar(
    @NonNull context: Context,
    @Nullable attrs: AttributeSet?,
    defStyleAttr: Int)

Public methods

collapseActionView

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

open fun dismissPopupMenus(): Unit

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

generateLayoutParams

open fun generateLayoutParams(attrs: AttributeSet!): Toolbar.LayoutParams!

getCollapseContentDescription

@Nullable 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 androidx.appcompat.R.attr#collapseContentDescription

getCollapseIcon

@Nullable open fun getCollapseIcon(): Drawable?

Return the current drawable used as the collapse icon.

Return
Drawable? The collapse icon drawable androidx.appcompat.R.attr#collapseIcon

getContentInsetEnd

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

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

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

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

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

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

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