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
ContextMenu.ContextMenuInfo
final boolean
static int
getDefaultSize(int size, int measureSpec)
Display
final int[]
Bitmap

This method is deprecated.

int

This method is deprecated.

int

This method is deprecated.

void
long
float
int
boolean
boolean
int
ArrayList<View>
getFocusables(int direction)
void
Drawable
int
BlendMode
ColorStateList
PorterDuff.Mode
boolean
getGlobalVisibleRect(Rect r, Point globalOffset)
Handler
float
float
float
float
Runnable
final boolean
final int
void
getHitRect(Rect outRect)
int
int
Drawable
Drawable
int
int
int
int
boolean
KeyEvent.DispatcherState
int
int
int
ViewGroup.LayoutParams
final int
float
int
final boolean
void
getLocationInSurface(int[] location)
void
getLocationInWindow(int[] outLocation)
void
getLocationOnScreen(int[] outLocation)
Matrix
final int
final int
final int
final int
final int
int
int
int
int
int
int
int
int
View.OnFocusChangeListener
int
ViewOutlineProvider
int
int
ViewOverlay
int
int
int
int
int
int
final ViewParent
ViewParent
float
float
PointerIcon
final List<Rect>
String[]
Resources
final boolean
final int
float
int
AttachedSurfaceControl
View
WindowInsets
float
float
float
float
float
int
int
int
int
int
int
final int
final int
int
int
final CharSequence
StateListAnimator
int
int
List<Rect>
int

This method is deprecated.

Object
int
int
CharSequence
final int
float
int
TouchDelegate
ArrayList<View>
float
String
float
float
float
long
int
int
Drawable
Drawable
int
ViewTranslationResponse
ViewTreeObserver
int
final int
int
WindowId
WindowInsetsController
int

This method is deprecated.

IBinder
int
void
float
float
float
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
static View
inflate(Context context, int resource, ViewGroup root)
void

This method is deprecated.

void
void
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean

This method is deprecated.

boolean
boolean
final boolean
final boolean
boolean
final boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
final boolean
final boolean
boolean
boolean
boolean
final boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
final boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
final boolean
boolean
boolean
final boolean
boolean
boolean
boolean
boolean
boolean
View
keyboardNavigationClusterSearch(View currentCluster, int direction)
final void
measure(int widthMeasureSpec, int heightMeasureSpec)
static int[]
mergeDrawableStates(int[] baseState, int[] additionalState)
void
offsetLeftAndRight(int offset)
void
offsetTopAndBottom(int offset)
void
void
WindowInsets
void
boolean
boolean
void
void
InputConnection
void
onCreateViewTranslationRequest(
    int[] supportedFormats,
    Consumer<ViewTranslationRequest> requestsCollector
)
void
onCreateVirtualViewTranslationRequests(
    long[] virtualIds,
    int[] supportedFormats,
    Consumer<ViewTranslationRequest> requestsCollector
)
void
onDisplayHint(int hint)
boolean
void
onDraw(Canvas canvas)
void
final void
boolean
void
void
void
onFocusChanged(
    boolean gainFocus,
    int direction,
    Rect previouslyFocusedRect
)
boolean
void
onHoverChanged(boolean hovered)
void
void
boolean
onKeyDown(int keyCode, KeyEvent event)
boolean
onKeyLongPress(int keyCode, KeyEvent event)
boolean
onKeyMultiple(int keyCode, int repeatCount, KeyEvent event)
boolean
onKeyPreIme(int keyCode, KeyEvent event)
boolean
onKeyShortcut(int keyCode, KeyEvent event)
boolean
onKeyUp(int keyCode, KeyEvent event)
void
onOverScrolled(
    int scrollX,
    int scrollY,
    boolean clampedX,
    boolean clampedY
)
void
onPointerCaptureChange(boolean hasCapture)
void
void
onProvideAutofillStructure(ViewStructure structure, int flags)
void
void
void
void
ContentInfo
void
onScreenStateChanged(int screenState)
void
onScrollCaptureSearch(
    Rect localVisibleRect,
    Point windowOffset,
    Consumer<ScrollCaptureTarget> targets
)
void
onScrollChanged(int l, int t, int oldl, int oldt)
boolean
onSetAlpha(int alpha)
void
onSizeChanged(int w, int h, int oldw, int oldh)
void
boolean
void
void
void
onVisibilityAggregated(boolean isVisible)
void
onVisibilityChanged(View changedView, int visibility)
void
onWindowFocusChanged(boolean hasWindowFocus)
void

This method is deprecated.

void
onWindowVisibilityChanged(int visibility)
boolean
overScrollBy(
    int deltaX,
    int deltaY,
    int scrollX,
    int scrollY,
    int scrollRangeX,
    int scrollRangeY,
    int maxOverScrollX,
    int maxOverScrollY,
    boolean isTouchEvent
)
boolean
performAccessibilityAction(int action, Bundle arguments)
boolean
boolean
performContextClick(float x, float y)
boolean
performHapticFeedback(int feedbackConstant)
boolean
ContentInfo
void
playSoundEffect(int soundConstant)
boolean
post(Runnable action)
boolean
postDelayed(Runnable action, long delayMillis)
void
void
postInvalidateDelayed(long delayMilliseconds)
void
void
void
postOnAnimationDelayed(Runnable action, long delayMillis)
void
void
boolean
void
void
void
void
void

This method is deprecated.

final boolean
void
void
boolean
final void
final T
<T extends View> requireViewById(int id)
void
static int
resolveSize(int size, int measureSpec)
static int
resolveSizeAndState(int size, int measureSpec, int childMeasuredState)
void
final void
saveAttributeDataForStyleable(
    Context context,
    int[] styleable,
    AttributeSet attrs,
    TypedArray t,
    int defStyleAttr,
    int defStyleRes
)
void
void
scheduleDrawable(Drawable who, Runnable what, long when)
void
scrollBy(int x, int y)
void
scrollTo(int x, int y)
void
sendAccessibilityEvent(int eventType)
void
void
setAccessibilityDataSensitive(int accessibilityDataSensitive)
void
void
setAccessibilityHeading(boolean isHeading)
void
void
setAccessibilityPaneTitle(CharSequence accessibilityPaneTitle)
void
void
void
setActivated(boolean activated)
void
setAllowClickWhenDisabled(boolean clickableWhenDisabled)
void
void
void
setAlpha(float alpha)
void
void
void
setAutoHandwritingEnabled(boolean enabled)
void
setAutofillHints(String[] autofillHints)
void
void
setBackground(Drawable background)
void
setBackgroundColor(int color)
void

This method is deprecated.

void
void
void
void
final void
setBottom(int bottom)
void
setCameraDistance(float distance)
void
setClickable(boolean clickable)
void
setClipBounds(Rect clipBounds)
void
setClipToOutline(boolean clipToOutline)
void
void
setContentDescription(CharSequence contentDescription)
void
setContextClickable(boolean contextClickable)
void
setDefaultFocusHighlightEnabled(boolean defaultFocusHighlightEnabled)
void

This method is deprecated.

void
setDrawingCacheEnabled(boolean enabled)

This method is deprecated.

void

This method is deprecated.

void
void
setElevation(float elevation)
void
setEnabled(boolean enabled)
void
setFadingEdgeLength(int length)
void
void
setFitsSystemWindows(boolean fitSystemWindows)
void
setFocusable(boolean focusable)
void
setFocusableInTouchMode(boolean focusableInTouchMode)
void
setFocusedByDefault(boolean isFocusedByDefault)
void
setForceDarkAllowed(boolean allow)
void
setForeground(Drawable foreground)
void
setForegroundGravity(int gravity)
void
void
void
void
setHandwritingBoundsOffsets(
    float offsetLeft,
    float offsetTop,
    float offsetRight,
    float offsetBottom
)
void
void
setHapticFeedbackEnabled(boolean hapticFeedbackEnabled)
void
setHasTransientState(boolean hasTransientState)
void
setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled)
void
setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled)
void
void
void
setHovered(boolean hovered)
void
setId(int id)
void
void
void
void
setIsCredential(boolean isCredential)
void
setIsHandwritingDelegate(boolean isHandwritingDelegate)
void
setKeepScreenOn(boolean keepScreenOn)
void
setKeyboardNavigationCluster(boolean isCluster)
void
setLabelFor(int id)
void
void
setLayerType(int layerType, Paint paint)
void
setLayoutDirection(int layoutDirection)
void
final void
setLeft(int left)
final void
setLeftTopRightBottom(int left, int top, int right, int bottom)
void
setLongClickable(boolean longClickable)
final void
setMeasuredDimension(int measuredWidth, int measuredHeight)
void
setMinimumHeight(int minHeight)
void
setMinimumWidth(int minWidth)
void
setNestedScrollingEnabled(boolean enabled)
void
setNextClusterForwardId(int nextClusterForwardId)
void
setNextFocusDownId(int nextFocusDownId)
void
setNextFocusForwardId(int nextFocusForwardId)
void
setNextFocusLeftId(int nextFocusLeftId)
void
setNextFocusRightId(int nextFocusRightId)
void
setNextFocusUpId(int nextFocusUpId)
void
void
void
void
void
void
void
void
void
void
void
void
setOnReceiveContentListener(
    String[] mimeTypes,
    OnReceiveContentListener listener
)
void
void

This method is deprecated.

void
void
void
void
void
setOverScrollMode(int overScrollMode)
void
setPadding(int left, int top, int right, int bottom)
void
setPaddingRelative(int start, int top, int end, int bottom)
void
setPivotX(float pivotX)
void
setPivotY(float pivotY)
void
final void
setPreferKeepClear(boolean preferKeepClear)
final void
void
setPressed(boolean pressed)
void
final void
setRevealOnFocusHint(boolean revealOnFocus)
final void
setRight(int right)
void
setRotation(float rotation)
void
setRotationX(float rotationX)
void
setRotationY(float rotationY)
void
setSaveEnabled(boolean enabled)
void
setSaveFromParentEnabled(boolean enabled)
void
setScaleX(float scaleX)
void
setScaleY(float scaleY)
void
setScreenReaderFocusable(boolean screenReaderFocusable)
void
setScrollBarDefaultDelayBeforeFade(
    int scrollBarDefaultDelayBeforeFade
)
void
setScrollBarFadeDuration(int scrollBarFadeDuration)
void
setScrollBarSize(int scrollBarSize)
void
setScrollBarStyle(int style)
final void
void
void
setScrollContainer(boolean isScrollContainer)
void
setScrollIndicators(int indicators)
void
setScrollX(int value)
void
setScrollY(int value)
void
setScrollbarFadingEnabled(boolean fadeScrollbars)
void
setSelected(boolean selected)
void
setSoundEffectsEnabled(boolean soundEffectsEnabled)
void
void
void
void
setSystemUiVisibility(int visibility)

This method is deprecated.

void
void
setTextAlignment(int textAlignment)
void
setTextDirection(int textDirection)
void
final void
setTop(int top)
void
void
setTransitionAlpha(float alpha)
final void
setTransitionName(String transitionName)
void
setTransitionVisibility(int visibility)
void
setTranslationX(float translationX)
void
setTranslationY(float translationY)
void
setTranslationZ(float translationZ)
void
setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled)
void
setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled)
void
void
void
void
void
setVisibility(int visibility)
void
setWillNotCacheDrawing(boolean willNotCacheDrawing)

This method is deprecated.

void
setWillNotDraw(boolean willNotDraw)
void
setX(float x)
void
setY(float y)
void
setZ(float z)
boolean
ActionMode
void
final boolean
startDrag(
    ClipData data,
    View.DragShadowBuilder shadowBuilder,
    Object myLocalState,
    int flags
)

This method is deprecated.

final boolean
startDragAndDrop(
    ClipData data,
    View.DragShadowBuilder shadowBuilder,
    Object myLocalState,
    int flags
)
boolean
void
String
void
void
void
final void
boolean
boolean

This method is deprecated.

boolean
From android.view.ViewGroup
void
void
addExtraDataToAccessibilityNodeInfo(
    AccessibilityNodeInfo info,
    String extraDataKey,
    Bundle arguments
)
void
addFocusables(ArrayList<View> views, int direction, int focusableMode)
void
boolean
void
void
addView(View child)
boolean
addViewInLayout(View child, int index, ViewGroup.LayoutParams params)
void
attachLayoutAnimationParameters(
    View child,
    ViewGroup.LayoutParams params,
    int index,
    int count
)
void
attachViewToParent(View child, int index, ViewGroup.LayoutParams params)
void
boolean
void
void
childHasTransientStateChanged(
    View child,
    boolean childHasTransientState
)
void
void
void
void
void
debug(int depth)
void
void
void
detachViewsFromParent(int start, int count)
WindowInsets
boolean
void
void
dispatchCreateViewTranslationRequest(
    Map<AutofillId, long[]> viewIds,
    int[] supportedFormats,
    TranslationCapability capability,
    List<ViewTranslationRequest> requests
)
void
boolean
void
void
dispatchDrawableHotspotChanged(float x, float y)
void
void
boolean
boolean
boolean
boolean
boolean
boolean
void
dispatchPointerCaptureChanged(boolean hasCapture)
void
void
void
void
void
dispatchScrollCaptureSearch(
    Rect localVisibleRect,
    Point windowOffset,
    Consumer<ScrollCaptureTarget> targets
)
void
dispatchSetActivated(boolean activated)
void
dispatchSetPressed(boolean pressed)
void
dispatchSetSelected(boolean selected)
void
void

This method is deprecated.

void
boolean
boolean
boolean
dispatchUnhandledMove(View focused, int direction)
void
dispatchVisibilityChanged(View changedView, int visibility)
void
dispatchWindowFocusChanged(boolean hasFocus)
void
void
WindowInsets
WindowInsetsAnimation.Bounds
void

This method is deprecated.

void
boolean
drawChild(Canvas canvas, View child, long drawingTime)
void
void
View
OnBackInvokedDispatcher
void
findViewsWithText(
    ArrayList<View> outViews,
    CharSequence text,
    int flags
)
View
focusSearch(View focused, int direction)
void
boolean
CharSequence
View
getChildAt(int index)
int
int
getChildDrawingOrder(int childCount, int drawingPosition)
static int
getChildMeasureSpec(int spec, int padding, int childDimension)
boolean
boolean
getChildVisibleRect(View child, Rect r, Point offset)
boolean
boolean
int
View
LayoutAnimationController
Animation.AnimationListener
int
LayoutTransition
int
ViewGroupOverlay
int

This method is deprecated.

boolean
boolean
boolean
int
final void
invalidateChild(View child, Rect dirty)

This method is deprecated.

ViewParent
invalidateChildInParent(int[] location, Rect dirty)

This method is deprecated.

boolean

This method is deprecated.

boolean

This method is deprecated.

boolean
boolean

This method is deprecated.

boolean
boolean
boolean
void
final void
layout(int l, int t, int r, int b)
void
measureChild(
    View child,
    int parentWidthMeasureSpec,
    int parentHeightMeasureSpec
)
void
measureChildWithMargins(
    View child,
    int parentWidthMeasureSpec,
    int widthUsed,
    int parentHeightMeasureSpec,
    int heightUsed
)
void
measureChildren(int widthMeasureSpec, int heightMeasureSpec)
void
notifySubtreeAccessibilityStateChanged(
    View child,
    View source,
    int changeType
)
final void
final void
int[]
onCreateDrawableState(int extraSpace)
void
boolean
boolean
boolean
onNestedFling(
    View target,
    float velocityX,
    float velocityY,
    boolean consumed
)
boolean
onNestedPreFling(View target, float velocityX, float velocityY)
boolean
onNestedPrePerformAccessibilityAction(
    View target,
    int action,
    Bundle args
)
void
onNestedPreScroll(View target, int dx, int dy, int[] consumed)
void
onNestedScroll(
    View target,
    int dxConsumed,
    int dyConsumed,
    int dxUnconsumed,
    int dyUnconsumed
)
void
onNestedScrollAccepted(View child, View target, int axes)
boolean
onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect)
boolean
PointerIcon
onResolvePointerIcon(MotionEvent event, int pointerIndex)
boolean
onStartNestedScroll(View child, View target, int nestedScrollAxes)
void
void
void
void
void
void
void
removeDetachedView(View child, boolean animate)
void
void
removeViewAt(int index)
void
void
removeViews(int start, int count)
void
removeViewsInLayout(int start, int count)
void
requestChildFocus(View child, View focused)
boolean
requestChildRectangleOnScreen(
    View child,
    Rect rectangle,
    boolean immediate
)
void
requestDisallowInterceptTouchEvent(boolean disallowIntercept)
boolean
requestFocus(int direction, Rect previouslyFocusedRect)
boolean
void
boolean
void
void
setAddStatesFromChildren(boolean addsStates)
void

This method is deprecated.

void
setAnimationCacheEnabled(boolean enabled)

This method is deprecated.

void

This method is deprecated.

void
void

This method is deprecated.

void
setClipChildren(boolean clipChildren)
void
setClipToPadding(boolean clipToPadding)
void
setDescendantFocusability(int focusability)
void
void
void
setLayoutMode(int layoutMode)
void
void
void
void
setPersistentDrawingCache(int drawingCacheToKeep)

This method is deprecated.

void
void
setTouchscreenBlocksFocus(boolean touchscreenBlocksFocus)
void
setTransitionGroup(boolean isTransitionGroup)
void
boolean
boolean
ActionMode
startActionModeForChild(
    View originalView,
    ActionMode.Callback callback
)
void
void
void
suppressLayout(boolean suppress)
void

Public constructors

Toolbar

Added in 1.1.0
public Toolbar(@NonNull Context context)

Toolbar

Added in 1.1.0
public Toolbar(@NonNull Context context, @Nullable AttributeSet attrs)

Toolbar

Added in 1.1.0
public Toolbar(
    @NonNull Context context,
    @Nullable AttributeSet attrs,
    int defStyleAttr
)

Public methods

addMenuProvider

Added in 1.4.0
@MainThread
public void addMenuProvider(@NonNull MenuProvider provider)

Adds the given MenuProvider to this MenuHost. If using this method, you must manually remove the provider when necessary.

Parameters
@NonNull MenuProvider provider

the MenuProvider to be added

addMenuProvider

Added in 1.4.0
@MainThread
public void addMenuProvider(
    @NonNull MenuProvider provider,
    @NonNull LifecycleOwner owner
)

Adds the given MenuProvider to this MenuHost. This MenuProvider will be removed once the given LifecycleOwner receives an Lifecycle.Event.ON_DESTROY event.

Parameters
@NonNull MenuProvider provider

the MenuProvider to be added

@NonNull LifecycleOwner owner

the Lifecycle owner whose state will determine the removal of the provider

addMenuProvider

Added in 1.4.0
@MainThread
public void 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. This MenuProvider will be removed once the given LifecycleOwner goes down from the given Lifecycle.State.

Parameters
@NonNull MenuProvider provider

the MenuProvider to be added

@NonNull LifecycleOwner owner

the Lifecycle owner whose state will be used for automated addition/removal

@NonNull Lifecycle.State state

the Lifecycle.State to check for automated addition/removal

collapseActionView

Added in 1.1.0
public void collapseActionView()

Collapse a currently expanded action view. If this Toolbar does not have an expanded action view this method has no effect.

An action view may be expanded either directly from the MenuItem it belongs to or by user action.

dismissPopupMenus

Added in 1.1.0
public void dismissPopupMenus()

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

generateLayoutParams

Added in 1.1.0
public Toolbar.LayoutParams generateLayoutParams(AttributeSet attrs)

getCollapseContentDescription

Added in 1.1.0
@Attribute(value = "androidx.appcompat:collapseContentDescription")
public @Nullable CharSequence getCollapseContentDescription()

Retrieve the currently configured content description for the collapse button view. This will be used to describe the collapse action to users through mechanisms such as screen readers or tooltips.

Returns
@Nullable CharSequence

The collapse button's content description collapseContentDescription

getCollapseIcon

Added in 1.1.0
@Attribute(value = "androidx.appcompat:collapseIcon")
public @Nullable Drawable getCollapseIcon()

Return the current drawable used as the collapse icon.

Returns
@Nullable Drawable

The collapse icon drawable collapseIcon

getContentInsetEnd

Added in 1.1.0
@Attribute(value = "androidx.appcompat:contentInsetEnd")
public int getContentInsetEnd()

Gets the ending content inset for this toolbar.

The content inset affects the valid area for Toolbar content other than the navigation button and menu. Insets define the minimum margin for these components and can be used to effectively align Toolbar content along well-known gridlines.

Returns
int

The ending content inset for this toolbar

getContentInsetEndWithActions

Added in 1.1.0
@Attribute(value = "androidx.appcompat:contentInsetEndWithActions")
public int getContentInsetEndWithActions()

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

Different content insets are often called for when additional buttons are present in the toolbar, as well as at different toolbar sizes. The larger value of getContentInsetEnd and this value will be used during layout.

Returns
int

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

getContentInsetLeft

Added in 1.1.0
@Attribute(value = "androidx.appcompat:contentInsetLeft")
public int getContentInsetLeft()

Gets the left content inset for this toolbar.

The content inset affects the valid area for Toolbar content other than the navigation button and menu. Insets define the minimum margin for these components and can be used to effectively align Toolbar content along well-known gridlines.

Returns
int

The left content inset for this toolbar

getContentInsetRight

Added in 1.1.0
@Attribute(value = "androidx.appcompat:contentInsetRight")
public int getContentInsetRight()

Gets the right content inset for this toolbar.

The content inset affects the valid area for Toolbar content other than the navigation button and menu. Insets define the minimum margin for these components and can be used to effectively align Toolbar content along well-known gridlines.

Returns
int

The right content inset for this toolbar

getContentInsetStart

Added in 1.1.0
@Attribute(value = "androidx.appcompat:contentInsetStart")
public int getContentInsetStart()

Gets the starting content inset for this toolbar.

The content inset affects the valid area for Toolbar content other than the navigation button and menu. Insets define the minimum margin for these components and can be used to effectively align Toolbar content along well-known gridlines.

Returns
int

The starting content inset for this toolbar

getContentInsetStartWithNavigation

Added in 1.1.0
@Attribute(value = "androidx.appcompat:contentInsetStartWithNavigation")
public int getContentInsetStartWithNavigation()

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

Different content insets are often called for when additional buttons are present in the toolbar, as well as at different toolbar sizes. The larger value of getContentInsetStart and this value will be used during layout.

Returns
int

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

getCurrentContentInsetEnd

Added in 1.1.0
public int getCurrentContentInsetEnd()

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

Returns
int

the current content inset end in pixels

getCurrentContentInsetLeft

Added in 1.1.0
public int getCurrentContentInsetLeft()

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

Returns
int

the current content inset left in pixels

getCurrentContentInsetRight

Added in 1.1.0
public int getCurrentContentInsetRight()

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

Returns
int

the current content inset right in pixels

getCurrentContentInsetStart

Added in 1.1.0
public int getCurrentContentInsetStart()

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

Returns
int

the current content inset start in pixels

getLogo

Added in 1.1.0
@Attribute(value = "androidx.appcompat:logo")
public Drawable getLogo()

Return the current logo drawable.

Returns
Drawable

The current logo drawable

See also
setLogo
setLogo

getLogoDescription

Added in 1.1.0
@Attribute(value = "androidx.appcompat:logoDescription")
public CharSequence getLogoDescription()

Return the description of the toolbar's logo.

Returns
CharSequence

A description of the logo

getMenu

Added in 1.1.0
@Attribute(value = "androidx.appcompat:menu")
public Menu getMenu()

Return the Menu shown in the toolbar.

Applications that wish to populate the toolbar's menu can do so from here. To use an XML menu resource, use inflateMenu.

Returns
Menu

The toolbar's Menu menu

getNavigationContentDescription

Added in 1.1.0
@Attribute(value = "androidx.appcompat:navigationContentDescription")
public @Nullable CharSequence getNavigationContentDescription()

Retrieve the currently configured content description for the navigation button view. This will be used to describe the navigation action to users through mechanisms such as screen readers or tooltips.

Returns
@Nullable CharSequence

The navigation button's content description navigationContentDescription

getNavigationIcon

Added in 1.1.0
@Attribute(value = "androidx.appcompat:navigationIcon")
public @Nullable Drawable getNavigationIcon()

Return the current drawable used as the navigation icon.

Returns
@Nullable Drawable

The navigation icon drawable navigationIcon

getOverflowIcon

Added in 1.1.0
public @Nullable Drawable getOverflowIcon()

Return the current drawable used as the overflow icon.

Returns
@Nullable Drawable

The overflow icon drawable

getPopupTheme

Added in 1.1.0
@Attribute(value = "androidx.appcompat:popupTheme")
public @StyleRes int getPopupTheme()
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

Added in 1.1.0
@Attribute(value = "androidx.appcompat:subtitle")
public CharSequence getSubtitle()

Return the subtitle of this toolbar.

Returns
CharSequence

The current subtitle

getTitle

Added in 1.1.0
@Attribute(value = "androidx.appcompat:title")
public CharSequence getTitle()

Returns the title of this toolbar.

Returns
CharSequence

The current title.

getTitleMarginBottom

Added in 1.1.0
@Attribute(value = "androidx.appcompat:titleMarginBottom")
public int getTitleMarginBottom()
Returns
int

the bottom title margin in pixels

getTitleMarginEnd

Added in 1.1.0
@Attribute(value = "androidx.appcompat:titleMarginEnd")
public int getTitleMarginEnd()
Returns
int

the ending title margin in pixels

getTitleMarginStart

Added in 1.1.0
@Attribute(value = "androidx.appcompat:titleMarginStart")
public int getTitleMarginStart()
Returns
int

the starting title margin in pixels

getTitleMarginTop

Added in 1.1.0
@Attribute(value = "androidx.appcompat:titleMarginTop")
public int getTitleMarginTop()
Returns
int

the top title margin in pixels

hasExpandedActionView

Added in 1.1.0
public boolean hasExpandedActionView()

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

An action view may be expanded either directly from the MenuItem it belongs to or by user action. If the Toolbar has an expanded action view it can be collapsed using the collapseActionView method.

Returns
boolean

true if the Toolbar has an expanded action view

hideOverflowMenu

Added in 1.1.0
public boolean hideOverflowMenu()

Hide the overflow items from the associated menu.

Returns
boolean

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

inflateMenu

Added in 1.1.0
public void inflateMenu(@MenuRes int resId)

Inflate a menu resource into this toolbar.

Inflate an XML menu resource into this toolbar. Existing items in the menu will not be modified or removed.

Parameters
@MenuRes int resId

ID of a menu resource to inflate menu

invalidateMenu

Added in 1.4.0
@MainThread
public void invalidateMenu()

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

Added in 1.6.0
public boolean isBackInvokedCallbackEnabled()

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.

isOverflowMenuShowing

Added in 1.1.0
public boolean isOverflowMenuShowing()

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

Returns
boolean

true if the overflow menu is currently showing

onHoverEvent

public boolean onHoverEvent(MotionEvent ev)

onRtlPropertiesChanged

public void onRtlPropertiesChanged(int layoutDirection)

onTouchEvent

public boolean onTouchEvent(MotionEvent ev)

removeMenuProvider

Added in 1.4.0
@MainThread
public void removeMenuProvider(@NonNull MenuProvider provider)

Removes the given MenuProvider from this MenuHost.

Parameters
@NonNull MenuProvider provider

the MenuProvider to be removed

setBackInvokedCallbackEnabled

Added in 1.6.0
public void setBackInvokedCallbackEnabled(boolean enabled)

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
boolean enabled

true to attempt to register a back invocation callback in supported configurations or false to not automatically handle back invocations

setCollapseContentDescription

Added in 1.1.0
public void setCollapseContentDescription(@Nullable CharSequence description)

Set a content description for the collapse button if one is present. The content description will be read via screen readers or other accessibility systems to explain the action of the navigation button.

Parameters
@Nullable CharSequence description

Content description to set, or null to clear the content description collapseContentDescription

setCollapseContentDescription

Added in 1.1.0
public void setCollapseContentDescription(@StringRes int resId)

Set a content description for the collapse button if one is present. The content description will be read via screen readers or other accessibility systems to explain the action of the collapse button.

Parameters
@StringRes int resId

Resource ID of a content description string to set, or 0 to clear the description collapseContentDescription

setCollapseIcon

Added in 1.1.0
public void setCollapseIcon(@Nullable Drawable icon)

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

The collapse button appears at the start of the toolbar when an action view is present .

Parameters
@Nullable Drawable icon

Drawable to set, may be null to use the default icon collapseIcon

setCollapseIcon

Added in 1.1.0
public void setCollapseIcon(@DrawableRes int resId)

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

The collapse button appears at the start of the toolbar when an action view is present .

Parameters
@DrawableRes int resId

Resource ID of a drawable to set collapseIcon

setContentInsetEndWithActions

Added in 1.1.0
public void setContentInsetEndWithActions(int insetEndWithActions)

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

Different content insets are often called for when additional buttons are present in the toolbar, as well as at different toolbar sizes. The larger value of getContentInsetEnd and this value will be used during layout.

Parameters
int insetEndWithActions

the inset to use when a menu has been set in pixels

setContentInsetStartWithNavigation

Added in 1.1.0
public void setContentInsetStartWithNavigation(int insetStartWithNavigation)

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

Different content insets are often called for when additional buttons are present in the toolbar, as well as at different toolbar sizes. The larger value of getContentInsetStart and this value will be used during layout.

Parameters
int insetStartWithNavigation

the inset to use when a navigation icon has been set in pixels

setContentInsetsAbsolute

Added in 1.1.0
public void setContentInsetsAbsolute(int contentInsetLeft, int contentInsetRight)

Sets the content insets for this toolbar.

The content inset affects the valid area for Toolbar content other than the navigation button and menu. Insets define the minimum margin for these components and can be used to effectively align Toolbar content along well-known gridlines.

Parameters
int contentInsetLeft

Content inset for the toolbar's left edge

int contentInsetRight

Content inset for the toolbar's right edge

setContentInsetsRelative

Added in 1.1.0
public void setContentInsetsRelative(int contentInsetStart, int contentInsetEnd)

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

The content inset affects the valid area for Toolbar content other than the navigation button and menu. Insets define the minimum margin for these components and can be used to effectively align Toolbar content along well-known gridlines.

Parameters
int contentInsetStart

Content inset for the toolbar starting edge

int contentInsetEnd

Content inset for the toolbar ending edge

setLogo

Added in 1.1.0
public void setLogo(Drawable drawable)

Set a logo drawable.

This drawable should generally take the place of title text. The logo cannot be clicked. Apps using a logo should also supply a description using setLogoDescription.

Parameters
Drawable drawable

Drawable to use as a logo

setLogo

Added in 1.1.0
public void setLogo(@DrawableRes int resId)

Set a logo drawable from a resource id.

This drawable should generally take the place of title text. The logo cannot be clicked. Apps using a logo should also supply a description using setLogoDescription.

Parameters
@DrawableRes int resId

ID of a drawable resource

setLogoDescription

Added in 1.1.0
public void setLogoDescription(CharSequence description)

Set a description of the toolbar's logo.

This description will be used for accessibility or other similar descriptions of the UI.

Parameters
CharSequence description

Description to set

setLogoDescription

Added in 1.1.0
public void setLogoDescription(@StringRes int resId)

Set a description of the toolbar's logo.

This description will be used for accessibility or other similar descriptions of the UI.

Parameters
@StringRes int resId

String resource id

setNavigationContentDescription

Added in 1.1.0
public void setNavigationContentDescription(@Nullable CharSequence description)

Set a content description for the navigation button if one is present. The content description will be read via screen readers or other accessibility systems to explain the action of the navigation button.

Parameters
@Nullable CharSequence description

Content description to set, or null to clear the content description navigationContentDescription

setNavigationContentDescription

Added in 1.1.0
public void setNavigationContentDescription(@StringRes int resId)

Set a content description for the navigation button if one is present. The content description will be read via screen readers or other accessibility systems to explain the action of the navigation button.

Parameters
@StringRes int resId

Resource ID of a content description string to set, or 0 to clear the description navigationContentDescription

setNavigationIcon

Added in 1.1.0
public void setNavigationIcon(@Nullable Drawable icon)

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

The navigation button appears at the start of the toolbar if present. Setting an icon will make the navigation button visible.

If you use a navigation icon you should also set a description for its action using setNavigationContentDescription. This is used for accessibility and tooltips.

Parameters
@Nullable Drawable icon

Drawable to set, may be null to clear the icon navigationIcon

setNavigationIcon

Added in 1.1.0
public void setNavigationIcon(@DrawableRes int resId)

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

The navigation button appears at the start of the toolbar if present. Setting an icon will make the navigation button visible.

If you use a navigation icon you should also set a description for its action using setNavigationContentDescription. This is used for accessibility and tooltips.

Parameters
@DrawableRes int resId

Resource ID of a drawable to set navigationIcon

setNavigationOnClickListener

Added in 1.1.0
public void setNavigationOnClickListener(View.OnClickListener listener)

Set a listener to respond to navigation events.

This listener will be called whenever the user clicks the navigation button at the start of the toolbar. An icon must be set for the navigation button to appear.

Parameters
View.OnClickListener listener

Listener to set

setOnMenuItemClickListener

Added in 1.1.0
public void setOnMenuItemClickListener(Toolbar.OnMenuItemClickListener listener)

Set a listener to respond to menu item click events.

This listener will be invoked whenever a user selects a menu item from the action buttons presented at the end of the toolbar or the associated overflow.

Parameters
Toolbar.OnMenuItemClickListener listener

Listener to set

setOverflowIcon

Added in 1.1.0
public void setOverflowIcon(@Nullable Drawable icon)

Set the icon to use for the overflow button.

Parameters
@Nullable Drawable icon

Drawable to set, may be null to clear the icon

setPopupTheme

Added in 1.1.0
public void setPopupTheme(@StyleRes int resId)

Specifies the theme to use when inflating popup menus. By default, uses the same theme as the toolbar itself.

Parameters
@StyleRes int resId

theme used to inflate popup menus

See also
getPopupTheme

setSubtitle

Added in 1.1.0
public void setSubtitle(@StringRes int resId)

Set the subtitle of this toolbar.

Subtitles should express extended information about the current content.

Parameters
@StringRes int resId

String resource ID

setSubtitle

Added in 1.1.0
public void setSubtitle(CharSequence subtitle)

Set the subtitle of this toolbar.

Subtitles should express extended information about the current content.

Parameters
CharSequence subtitle

Subtitle to set

setSubtitleTextAppearance

Added in 1.1.0
public void setSubtitleTextAppearance(Context context, @StyleRes int resId)

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

setSubtitleTextColor

Added in 1.1.0
public void setSubtitleTextColor(@NonNull ColorStateList color)

Sets the text color of the subtitle, if present.

Parameters
@NonNull ColorStateList color

The new text color

setSubtitleTextColor

Added in 1.1.0
public void setSubtitleTextColor(@ColorInt int color)

Sets the text color of the subtitle, if present.

Parameters
@ColorInt int color

The new text color in 0xAARRGGBB format

setTitle

Added in 1.1.0
public void setTitle(@StringRes int resId)

Set the title of this toolbar.

A title should be used as the anchor for a section of content. It should describe or name the content being viewed.

Parameters
@StringRes int resId

Resource ID of a string to set as the title

setTitle

Added in 1.1.0
public void setTitle(CharSequence title)

Set the title of this toolbar.

A title should be used as the anchor for a section of content. It should describe or name the content being viewed.

Parameters
CharSequence title

Title to set

setTitleMargin

Added in 1.1.0
public void setTitleMargin(int start, int top, int end, int bottom)

Sets the title margin.

Parameters
int start

the starting title margin in pixels

int top

the top title margin in pixels

int end

the ending title margin in pixels

int bottom

the bottom title margin in pixels

setTitleMarginBottom

Added in 1.1.0
public void setTitleMarginBottom(int margin)

Sets the bottom title margin in pixels.

Parameters
int margin

the bottom title margin in pixels

setTitleMarginEnd

Added in 1.1.0
public void setTitleMarginEnd(int margin)

Sets the ending title margin in pixels.

Parameters
int margin

the ending title margin in pixels

setTitleMarginStart

Added in 1.1.0
public void setTitleMarginStart(int margin)

Sets the starting title margin in pixels.

Parameters
int margin

the starting title margin in pixels

setTitleMarginTop

Added in 1.1.0
public void setTitleMarginTop(int margin)

Sets the top title margin in pixels.

Parameters
int margin

the top title margin in pixels

setTitleTextAppearance

Added in 1.1.0
public void setTitleTextAppearance(Context context, @StyleRes int resId)

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

setTitleTextColor

Added in 1.1.0
public void setTitleTextColor(@NonNull ColorStateList color)

Sets the text color of the title, if present.

Parameters
@NonNull ColorStateList color

The new text color

setTitleTextColor

Added in 1.1.0
public void setTitleTextColor(@ColorInt int color)

Sets the text color of the title, if present.

Parameters
@ColorInt int color

The new text color in 0xAARRGGBB format

showOverflowMenu

Added in 1.1.0
public boolean showOverflowMenu()

Show the overflow items from the associated menu.

Returns
boolean

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

Protected methods

checkLayoutParams

protected boolean checkLayoutParams(ViewGroup.LayoutParams p)

generateDefaultLayoutParams

Added in 1.1.0
protected Toolbar.LayoutParams generateDefaultLayoutParams()

generateLayoutParams

Added in 1.1.0
protected Toolbar.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p)

onAttachedToWindow

protected void onAttachedToWindow()

onDetachedFromWindow

protected void onDetachedFromWindow()

onLayout

protected void onLayout(boolean changed, int l, int t, int r, int b)

onMeasure

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

onRestoreInstanceState

protected void onRestoreInstanceState(Parcelable state)

onSaveInstanceState

protected Parcelable onSaveInstanceState()

Extension functions

ToolbarKt.setupWithNavController

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

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
@NonNull NavController navController

The NavController whose navigation actions will be reflected in the title of the Toolbar.

@NonNull AppBarConfiguration configuration

Additional configuration options for customizing the behavior of the Toolbar

ToolbarKt.setupWithNavController

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

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
@NonNull 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