Toolbar


public class Toolbar extends ViewGroup implements 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 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.

buttonGravitycollapseContentDescriptioncollapseIconcontentInsetEndcontentInsetLeftcontentInsetRightcontentInsetStartcontentInsetStartWithNavigationcontentInsetEndWithActionsgravitylogologoDescriptionmaxButtonHeightnavigationContentDescriptionnavigationIconpopupThemesubtitlesubtitleTextAppearancesubtitleTextColortitletitleMargintitleMarginBottomtitleMarginEndtitleMarginStarttitleMarginToptitleTextAppearancetitleTextColormenu

Summary

Nested types

Layout information for child views of Toolbars.

Interface responsible for receiving menu item click events if the items themselves do not have individual item click listeners.

public class Toolbar.SavedState extends AbsSavedState

Public constructors

Toolbar(
    @NonNull Context context,
    @Nullable AttributeSet attrs,
    int defStyleAttr
)

Public methods

void

Adds the given MenuProvider to this MenuHost.

void

Adds the given MenuProvider to this MenuHost.

void
@MainThread
addMenuProvider(
    @NonNull MenuProvider provider,
    @NonNull LifecycleOwner owner,
    @NonNull Lifecycle.State state
)

Adds the given MenuProvider to this MenuHost once the given LifecycleOwner reaches the given Lifecycle.State.

void

Collapse a currently expanded action view.

void

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

Toolbar.LayoutParams
@Nullable CharSequence
@Attribute(value = "androidx.appcompat:collapseContentDescription")
getCollapseContentDescription()

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

@Nullable Drawable
@Attribute(value = "androidx.appcompat:collapseIcon")
getCollapseIcon()

Return the current drawable used as the collapse icon.

int
@Attribute(value = "androidx.appcompat:contentInsetEnd")
getContentInsetEnd()

Gets the ending content inset for this toolbar.

int
@Attribute(value = "androidx.appcompat:contentInsetEndWithActions")
getContentInsetEndWithActions()

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

int
@Attribute(value = "androidx.appcompat:contentInsetLeft")
getContentInsetLeft()

Gets the left content inset for this toolbar.

int
@Attribute(value = "androidx.appcompat:contentInsetRight")
getContentInsetRight()

Gets the right content inset for this toolbar.

int
@Attribute(value = "androidx.appcompat:contentInsetStart")
getContentInsetStart()

Gets the starting content inset for this toolbar.

int
@Attribute(value = "androidx.appcompat:contentInsetStartWithNavigation")
getContentInsetStartWithNavigation()

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
@Attribute(value = "androidx.appcompat:logo")
getLogo()

Return the current logo drawable.

CharSequence
@Attribute(value = "androidx.appcompat:logoDescription")
getLogoDescription()

Return the description of the toolbar's logo.

Menu
@Attribute(value = "androidx.appcompat:menu")
getMenu()

Return the Menu shown in the toolbar.

@Nullable CharSequence
@Attribute(value = "androidx.appcompat:navigationContentDescription")
getNavigationContentDescription()

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

@Nullable Drawable
@Attribute(value = "androidx.appcompat:navigationIcon")
getNavigationIcon()

Return the current drawable used as the navigation icon.

@Nullable Drawable

Return the current drawable used as the overflow icon.

@StyleRes int
@Attribute(value = "androidx.appcompat:popupTheme")
getPopupTheme()
CharSequence
@Attribute(value = "androidx.appcompat:subtitle")
getSubtitle()

Return the subtitle of this toolbar.

CharSequence
@Attribute(value = "androidx.appcompat:title")
getTitle()

Returns the title of this toolbar.

int
@Attribute(value = "androidx.appcompat:titleMarginBottom")
getTitleMarginBottom()
int
@Attribute(value = "androidx.appcompat:titleMarginEnd")
getTitleMarginEnd()
int
@Attribute(value = "androidx.appcompat:titleMarginStart")
getTitleMarginStart()
int
@Attribute(value = "androidx.appcompat:titleMarginTop")
getTitleMarginTop()
boolean

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

boolean

Hide the overflow items from the associated menu.

void
inflateMenu(@MenuRes int resId)

Inflate a menu resource into this toolbar.

void

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 OnBackInvokedCallback in supported configurations to handle collapsing expanded action items when a back invocation occurs.

boolean

Check whether the overflow menu is currently showing.

boolean
void
onRtlPropertiesChanged(int layoutDirection)
boolean
void

Removes the given MenuProvider from this MenuHost.

void

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.

void

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

void

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

void

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

void

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

void
setContentInsetEndWithActions(int insetEndWithActions)

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

void
setContentInsetStartWithNavigation(int insetStartWithNavigation)

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

void
setContentInsetsAbsolute(int contentInsetLeft, int contentInsetRight)

Sets the content insets for this toolbar.

void
setContentInsetsRelative(int contentInsetStart, int contentInsetEnd)

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

void
setLogo(Drawable drawable)

Set a logo drawable.

void
setLogo(@DrawableRes int resId)

Set a logo drawable from a resource id.

void

Set a description of the toolbar's logo.

void

Set a description of the toolbar's logo.

void

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

void

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

void

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

void

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

void

Set a listener to respond to navigation events.

void

Set a listener to respond to menu item click events.

void

Set the icon to use for the overflow button.

void
setPopupTheme(@StyleRes int resId)

Specifies the theme to use when inflating popup menus.

void
setSubtitle(@StringRes int resId)

Set the subtitle of this toolbar.

void

Set the subtitle of this toolbar.

void

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

void

Sets the text color of the subtitle, if present.

void

Sets the text color of the subtitle, if present.

void
setTitle(@StringRes int resId)

Set the title of this toolbar.

void

Set the title of this toolbar.

void
setTitleMargin(int start, int top, int end, int bottom)

Sets the title margin.

void

Sets the bottom title margin in pixels.

void
setTitleMarginEnd(int margin)

Sets the ending title margin in pixels.

void
setTitleMarginStart(int margin)

Sets the starting title margin in pixels.

void
setTitleMarginTop(int margin)

Sets the top title margin in pixels.

void
setTitleTextAppearance(Context context, @StyleRes int resId)

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

void

Sets the text color of the title, if present.

void

Sets the text color of the title, if present.

boolean

Show the overflow items from the associated menu.

Protected methods

boolean
Toolbar.LayoutParams
Toolbar.LayoutParams
void
void
void
onLayout(boolean changed, int l, int t, int r, int b)
void
onMeasure(int widthMeasureSpec, int heightMeasureSpec)
void
Parcelable

Extension functions

final void
ToolbarKt.setupWithNavController(
    @NonNull Toolbar receiver,
    @NonNull NavController navController,
    @NonNull AppBarConfiguration configuration
)

Sets up a Toolbar for use with a NavController.

final void
ToolbarKt.setupWithNavController(
    @NonNull Toolbar receiver,
    @NonNull NavController navController,
    DrawerLayout drawerLayout
)

Sets up a Toolbar for use with a NavController.

Inherited Constants

From android.view.View
static final int
static final int
static final int
static final int
static final int
static final int
static final Property<ViewFloat>
static final int
static final String
AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DATE = "creditCardExpirationDate"
static final String
AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DAY = "creditCardExpirationDay"
static final String
AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_MONTH = "creditCardExpirationMonth"
static final String
AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_YEAR = "creditCardExpirationYear"
static final String
static final String
AUTOFILL_HINT_CREDIT_CARD_SECURITY_CODE = "creditCardSecurityCode"
static final String
static final String
static final String
static final String
static final String
static final String
static final String
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int
static final int
static final int
static final int
static final int
static final int
static final int[]
static final int[]
static final int[]
static final int[]
static final int
static final int
static final int
static final int
static final int
static final int
static final int
GONE = 8
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
KEEP_SCREEN_ON = 67108864
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
NO_ID = -1
static final int
static final int
static final int
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final Property<ViewFloat>
static final Property<ViewFloat>
static final Property<ViewFloat>
static final Property<ViewFloat>
static final Property<ViewFloat>
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int[]
static final int[]
static final int
static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final Property<ViewFloat>
static final Property<ViewFloat>
static final Property<ViewFloat>
static final String
VIEW_LOG_TAG = "View"
static final int
static final int[]
static final Property<ViewFloat>
static final Property<ViewFloat>
static final Property<ViewFloat>
From android.view.ViewGroup
static final int
static final int
static final int
static final int
static final int
static final int
static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

Inherited methods

From android.view.View
void
void
void
ViewPropertyAnimator
void
void
boolean
void
void

This method is deprecated.

void
boolean
boolean
boolean
boolean
boolean
canScrollHorizontally(int direction)
boolean
canScrollVertically(int direction)
final void
void
final void
boolean
void
void
static int
combineMeasuredStates(int curState, int newState)
int
int
int
void
WindowInsets
int
int
int
AccessibilityNodeInfo
void
void

This method is deprecated.

boolean
boolean
dispatchNestedFling(float velocityX, float velocityY, boolean consumed)
boolean
dispatchNestedPreFling(float velocityX, float velocityY)
boolean
dispatchNestedPrePerformAccessibilityAction(
    int action,
    Bundle arguments
)
boolean
dispatchNestedPreScroll(
    int dx,
    int dy,
    int[] consumed,
    int[] offsetInWindow
)
boolean
dispatchNestedScroll(
    int dxConsumed,
    int dyConsumed,
    int dxUnconsumed,
    int dyUnconsumed,
    int[] offsetInWindow
)
boolean
void
draw(Canvas canvas)
void
drawableHotspotChanged(float x, float y)
final OnBackInvokedDispatcher
final T
<T extends View> findViewById(int id)
final T
<T extends View> findViewWithTag(Object tag)
boolean

This method is deprecated.

void
forceHasOverlappingRendering(boolean hasOverlappingRendering)
void
void
generateDisplayHash(
    String hashAlgorithm,
    Rect bounds,
    Executor executor,
    DisplayHashResultCallback callback
)
static int
View.AccessibilityDelegate
int
AccessibilityNodeProvider
CharSequence
int
int
String
String
float
Animation
Matrix
IBinder
int[]
Map<IntegerInteger>
String[]
final AutofillId
int
AutofillValue
Drawable
BlendMode
ColorStateList
PorterDuff.Mode
int
final int
float
int
float
Rect
boolean
final boolean
final ContentCaptureSession
CharSequence
final Context
C