BottomAppBar
public
class
BottomAppBar
extends Toolbar
implements
CoordinatorLayout.AttachedBehavior
java.lang.Object | ||||
↳ | android.view.View | |||
↳ | android.view.ViewGroup | |||
↳ | androidx.appcompat.widget.Toolbar | |||
↳ | com.google.android.material.bottomappbar.BottomAppBar |
The Bottom App Bar is an extension of Toolbar that supports a shaped background that "cradles" an
attached FloatingActionButton
. A FAB is anchored to BottomAppBar
by calling
CoordinatorLayout.LayoutParams.setAnchorId(int)
, or by setting app:layout_anchor
on the FAB in xml.
Note: The Material Design Guidelines caution against using an ExtendedFloatingActionButton
with a BottomAppBar
, so there is limited support for that
use case. ExtendedFloatingActionButton
can be anchored to the BottomAppBar
, but
currently animations and the cutout are not supported.
There are two modes which determine where the FAB is shown relative to the BottomAppBar
. FAB_ALIGNMENT_MODE_CENTER
mode is the primary mode with the FAB is
centered. FAB_ALIGNMENT_MODE_END
is the secondary mode with the FAB on the side.
Do not use the android:background
attribute or call BottomAppBar.setBackground
because the BottomAppBar manages its background internally. Instead use app:backgroundTint
.
To enable color theming for menu items you will also need to set the materialThemeOverlay
attribute to a ThemeOverlay which sets the colorControlNormal
attribute to the correct color. For example, if the background of the BottomAppBar is colorSurface
, as it is in the default style, you should set materialThemeOverlay
to
@style/ThemeOverlay.MaterialComponents.BottomAppBar.Surface
.
For more information, see the component developer guidance and design guidelines.
Summary
Nested classes | |
---|---|
class |
BottomAppBar.Behavior
Behavior designed for use with |
@interface |
BottomAppBar.FabAlignmentMode
The fabAlignmentMode determines the horizontal positioning of the cradle and the FAB which can be centered or aligned to the end. |
@interface |
BottomAppBar.FabAnimationMode
The fabAnimationMode determines the animation used to move the FAB between different alignment modes. |
XML attributes | |
---|---|
BottomAppBar_backgroundTint |
|
BottomAppBar_fabAlignmentMode |
|
BottomAppBar_fabAnchorMode |
|
BottomAppBar_fabAnimationMode |
|
BottomAppBar_fabCradleMargin |
|
BottomAppBar_fabCradleRoundedCornerRadius |
|
BottomAppBar_fabCradleVerticalOffset |
|
BottomAppBar_hideOnScroll |
|
BottomAppBar_paddingBottomSystemWindowInsets |
Constants | |
---|---|
int |
FAB_ALIGNMENT_MODE_CENTER
|
int |
FAB_ALIGNMENT_MODE_END
|
int |
FAB_ANCHOR_MODE_CRADLE
The FAB is cradled at the top of the BottomAppBar. |
int |
FAB_ANCHOR_MODE_EMBED
The FAB is embedded inside the BottomAppBar. |
int |
FAB_ANIMATION_MODE_SCALE
|
int |
FAB_ANIMATION_MODE_SLIDE
|
int |
MENU_ALIGNMENT_MODE_AUTO
The menu items are aligned automatically to avoid the FAB. |
int |
MENU_ALIGNMENT_MODE_START
The menu items are aligned to the start. |
Inherited constants |
---|
Inherited fields |
---|
Public constructors | |
---|---|
BottomAppBar(Context context)
|
|
BottomAppBar(Context context, AttributeSet attrs)
|
|
BottomAppBar(Context context, AttributeSet attrs, int defStyleAttr)
|
Public methods | |
---|---|
void
|
addOnScrollStateChangedListener(HideBottomViewOnScrollBehavior.OnScrollStateChangedListener listener)
Add a listener that will be called when the bottom app bar scroll state changes. |
void
|
clearOnScrollStateChangedListeners()
Remove all previously added |
ColorStateList
|
getBackgroundTint()
|
BottomAppBar.Behavior
|
getBehavior()
|
float
|
getCradleVerticalOffset()
Returns the vertical offset for the fab cutout. |
int
|
getFabAlignmentMode()
Returns the current fabAlignmentMode, either |
int
|
getFabAlignmentModeEndMargin()
Returns the |
int
|
getFabAnchorMode()
Returns the current |
int
|
getFabAnimationMode()
Returns the current |
float
|
getFabCradleMargin()
Returns the cradle margin for the fab cutout. |
float
|
getFabCradleRoundedCornerRadius()
Returns the rounded corner radius for the cutout if it exists. |
boolean
|
getHideOnScroll()
Returns true if the |
int
|
getMenuAlignmentMode()
Returns the current menuAlignmentMode, either |
boolean
|
isScrolledDown()
Returns true if the |
boolean
|
isScrolledUp()
Returns true if the |
void
|
performHide()
Animates the |
void
|
performHide(boolean animate)
Hides the |
void
|
performShow(boolean animate)
Shows the |
void
|
performShow()
Animates the |
void
|
removeOnScrollStateChangedListener(HideBottomViewOnScrollBehavior.OnScrollStateChangedListener listener)
Remove a listener that was previously added via |
void
|
replaceMenu(int newMenu)
A convenience method to replace the contents of the BottomAppBar's menu. |
void
|
setBackgroundTint(ColorStateList backgroundTint)
|
void
|
setCradleVerticalOffset(float verticalOffset)
Sets the vertical offset, in pixels, of the |
void
|
setElevation(float elevation)
|
void
|
setFabAlignmentMode(int fabAlignmentMode)
Sets the current |
void
|
setFabAlignmentModeAndReplaceMenu(int fabAlignmentMode, int newMenu)
Sets the current |
void
|
setFabAlignmentModeEndMargin(int margin)
Sets the end margin, in pixels, of the |
void
|
setFabAnchorMode(int fabAnchorMode)
Sets the current |
void
|
setFabAnimationMode(int fabAnimationMode)
Sets the current |
void
|
setFabCradleMargin(float cradleMargin)
Sets the cradle margin for the fab cutout. |
void
|
setFabCradleRoundedCornerRadius(float roundedCornerRadius)
Sets the rounded corner radius for the fab cutout. |
void
|
setHideOnScroll(boolean hide)
Sets if the |
void
|
setMenuAlignmentMode(int menuAlignmentMode)
Sets the current |
void
|
setNavigationIcon(Drawable drawable)
|
void
|
setNavigationIconTint(int navigationIconTint)
Sets the color of the toolbar's navigation icon. |
void
|
setSubtitle(CharSequence subtitle)
|
void
|
setTitle(CharSequence title)
|
Protected methods | |
---|---|
void
|
createFabDefaultXAnimation(int targetMode, List<Animator> animators)
Creates the default animation for moving a fab between alignment modes. |
int
|
getActionMenuViewTranslationX(ActionMenuView actionMenuView, int fabAlignmentMode, boolean fabAttached)
Returns the X translation to position the |
void
|
onAttachedToWindow()
|
void
|
onLayout(boolean changed, int l, int t, int r, int b)
|
void
|
onRestoreInstanceState(Parcelable state)
|
Parcelable
|
onSaveInstanceState()
|
Inherited methods | |
---|---|
XML attributes
BottomAppBar_backgroundTint
BottomAppBar_fabAlignmentMode
BottomAppBar_fabAnchorMode
BottomAppBar_fabAnimationMode
BottomAppBar_fabCradleMargin
BottomAppBar_fabCradleRoundedCornerRadius
BottomAppBar_fabCradleVerticalOffset
BottomAppBar_hideOnScroll
BottomAppBar_paddingBottomSystemWindowInsets
Constants
FAB_ALIGNMENT_MODE_CENTER
public static final int FAB_ALIGNMENT_MODE_CENTER
Constant Value: 0 (0x00000000)
FAB_ALIGNMENT_MODE_END
public static final int FAB_ALIGNMENT_MODE_END
Constant Value: 1 (0x00000001)
FAB_ANCHOR_MODE_CRADLE
public static final int FAB_ANCHOR_MODE_CRADLE
The FAB is cradled at the top of the BottomAppBar.
Constant Value: 1 (0x00000001)
FAB_ANCHOR_MODE_EMBED
public static final int FAB_ANCHOR_MODE_EMBED
The FAB is embedded inside the BottomAppBar.
Constant Value: 0 (0x00000000)
FAB_ANIMATION_MODE_SCALE
public static final int FAB_ANIMATION_MODE_SCALE
Constant Value: 0 (0x00000000)
FAB_ANIMATION_MODE_SLIDE
public static final int FAB_ANIMATION_MODE_SLIDE
Constant Value: 1 (0x00000001)
MENU_ALIGNMENT_MODE_AUTO
public static final int MENU_ALIGNMENT_MODE_AUTO
The menu items are aligned automatically to avoid the FAB.
Constant Value: 0 (0x00000000)
MENU_ALIGNMENT_MODE_START
public static final int MENU_ALIGNMENT_MODE_START
The menu items are aligned to the start.
Constant Value: 1 (0x00000001)
Public constructors
BottomAppBar
public BottomAppBar (Context context)
Parameters | |
---|---|
context |
Context |
BottomAppBar
public BottomAppBar (Context context, AttributeSet attrs)
Parameters | |
---|---|
context |
Context |
attrs |
AttributeSet |
BottomAppBar
public BottomAppBar (Context context, AttributeSet attrs, int defStyleAttr)
Parameters | |
---|---|
context |
Context |
attrs |
AttributeSet |
defStyleAttr |
int |
Public methods
addOnScrollStateChangedListener
public void addOnScrollStateChangedListener (HideBottomViewOnScrollBehavior.OnScrollStateChangedListener listener)
Add a listener that will be called when the bottom app bar scroll state changes.
See HideBottomViewOnScrollBehavior.OnScrollStateChangedListener
.
Components that add a listener should take care to remove it when finished via removeOnScrollStateChangedListener(OnScrollStateChangedListener)
.
Parameters | |
---|---|
listener |
HideBottomViewOnScrollBehavior.OnScrollStateChangedListener : listener to add
|
clearOnScrollStateChangedListeners
public void clearOnScrollStateChangedListeners ()
Remove all previously added HideBottomViewOnScrollBehavior.OnScrollStateChangedListener
s.
getBackgroundTint
public ColorStateList getBackgroundTint ()
Returns | |
---|---|
ColorStateList |
getCradleVerticalOffset
public float getCradleVerticalOffset ()
Returns the vertical offset for the fab cutout. An offset of 0 indicates the vertical center of
the FloatingActionButton
is positioned on the top edge.
Returns | |
---|---|
float |
getFabAlignmentMode
public int getFabAlignmentMode ()
Returns the current fabAlignmentMode, either FAB_ALIGNMENT_MODE_CENTER
or FAB_ALIGNMENT_MODE_END
.
Returns | |
---|---|
int |
getFabAlignmentModeEndMargin
public int getFabAlignmentModeEndMargin ()
Returns the FloatingActionButton
end margin pixel offset for the fab if it is set.
An end margin of -1 indicates that the default margin will be used.
Returns | |
---|---|
int |
getFabAnchorMode
public int getFabAnchorMode ()
Returns the current fabAnchorMode
, either FAB_ANCHOR_MODE_CRADLE
or FAB_ANCHOR_MODE_EMBED
.
Returns | |
---|---|
int |
getFabAnimationMode
public int getFabAnimationMode ()
Returns the current fabAnimationMode
, either FAB_ANIMATION_MODE_SCALE
or
FAB_ANIMATION_MODE_SLIDE
.
Returns | |
---|---|
int |
getFabCradleMargin
public float getFabCradleMargin ()
Returns the cradle margin for the fab cutout. This is the space between the fab and the cutout.
Returns | |
---|---|
float |
getFabCradleRoundedCornerRadius
public float getFabCradleRoundedCornerRadius ()
Returns the rounded corner radius for the cutout if it exists. A value of 0 will be a sharp edge.
Returns | |
---|---|
float |
getHideOnScroll
public boolean getHideOnScroll ()
Returns true if the BottomAppBar
should hide when a NestedScrollingChild
is scrolled. This is handled by BottomAppBar.Behavior
.
Returns | |
---|---|
boolean |
getMenuAlignmentMode
public int getMenuAlignmentMode ()
Returns the current menuAlignmentMode, either MENU_ALIGNMENT_MODE_AUTO
or MENU_ALIGNMENT_MODE_START
.
Returns | |
---|---|
int |
isScrolledDown
public boolean isScrolledDown ()
Returns true if the BottomAppBar
is scrolled down.
Returns | |
---|---|
boolean |
isScrolledUp
public boolean isScrolledUp ()
Returns true if the BottomAppBar
is scrolled up.
Returns | |
---|---|
boolean |
performHide
public void performHide (boolean animate)
Hides the BottomAppBar
.
Parameters | |
---|---|
animate |
boolean : false to hide the BottomAppBar immediately without animation.
|
performShow
public void performShow (boolean animate)
Shows the BottomAppBar
.
Parameters | |
---|---|
animate |
boolean : false to show the BottomAppBar immediately without animation.
|
removeOnScrollStateChangedListener
public void removeOnScrollStateChangedListener (HideBottomViewOnScrollBehavior.OnScrollStateChangedListener listener)
Remove a listener that was previously added via addOnScrollStateChangedListener(OnScrollStateChangedListener)
.
Parameters | |
---|---|
listener |
HideBottomViewOnScrollBehavior.OnScrollStateChangedListener : listener to remove
|
replaceMenu
public void replaceMenu (int newMenu)
A convenience method to replace the contents of the BottomAppBar's menu.
Parameters | |
---|---|
newMenu |
int : the desired new menu.
|
setBackgroundTint
public void setBackgroundTint (ColorStateList backgroundTint)
Parameters | |
---|---|
backgroundTint |
ColorStateList |
setCradleVerticalOffset
public void setCradleVerticalOffset (float verticalOffset)
Sets the vertical offset, in pixels, of the FloatingActionButton
being cradled. An
offset of 0 indicates the vertical center of the FloatingActionButton
is positioned on
the top edge.
This will not be visible until there is a cradle.
Parameters | |
---|---|
verticalOffset |
float |
setElevation
public void setElevation (float elevation)
Parameters | |
---|---|
elevation |
float |
setFabAlignmentMode
public void setFabAlignmentMode (int fabAlignmentMode)
Sets the current fabAlignmentMode
. An animated transition between current and desired
modes will be played.
Parameters | |
---|---|
fabAlignmentMode |
int : the desired fabAlignmentMode, either FAB_ALIGNMENT_MODE_CENTER
or FAB_ALIGNMENT_MODE_END .
|
setFabAlignmentModeAndReplaceMenu
public void setFabAlignmentModeAndReplaceMenu (int fabAlignmentMode, int newMenu)
Sets the current fabAlignmentMode
and replaces the BottomAppBar
's menu
resource. An animated transition between the current and desired mode will be played in
coordination with a menu resource swap animation.
Parameters | |
---|---|
fabAlignmentMode |
int : the desired fabAlignmentMode, either FAB_ALIGNMENT_MODE_CENTER
or FAB_ALIGNMENT_MODE_END . |
newMenu |
int : the menu resource of a new menu to be inflated and swapped during the animation.
Passing 0 for newMenu will not clear the menu but will skip all menu manipulation. If you'd
like to animate the FAB's alignment and clear the menu at the same time, use getMenu().clear() and setFabAlignmentMode(int) .
|
setFabAlignmentModeEndMargin
public void setFabAlignmentModeEndMargin (int margin)
Sets the end margin, in pixels, of the FloatingActionButton
. This will only have an
effect if the fab alignment mode is FAB_ALIGNMENT_MODE_END
.
An offset of -1 will use the default margin.
Parameters | |
---|---|
margin |
int |
setFabAnchorMode
public void setFabAnchorMode (int fabAnchorMode)
Sets the current fabAnchorMode
.
Parameters | |
---|---|
fabAnchorMode |
int : the desired fabAnchorMode, either FAB_ANCHOR_MODE_CRADLE or
FAB_ANCHOR_MODE_EMBED .
|
setFabAnimationMode
public void setFabAnimationMode (int fabAnimationMode)
Sets the current fabAnimationMode
.
Parameters | |
---|---|
fabAnimationMode |
int : the desired fabAlignmentMode, either FAB_ALIGNMENT_MODE_CENTER
or FAB_ALIGNMENT_MODE_END .
|
setFabCradleMargin
public void setFabCradleMargin (float cradleMargin)
Sets the cradle margin for the fab cutout. This is the space between the fab and the cutout. If the fab anchor mode is not cradled, this will not be respected.
Parameters | |
---|---|
cradleMargin |
float |
setFabCradleRoundedCornerRadius
public void setFabCradleRoundedCornerRadius (float roundedCornerRadius)
Sets the rounded corner radius for the fab cutout. A value of 0 will be a sharp edge. This will not be visible until there is a cradle.
Parameters | |
---|---|
roundedCornerRadius |
float |
setHideOnScroll
public void setHideOnScroll (boolean hide)
Sets if the BottomAppBar
should hide when a NestedScrollingChild
is scrolled. This is handled by BottomAppBar.Behavior
.
Parameters | |
---|---|
hide |
boolean |
setMenuAlignmentMode
public void setMenuAlignmentMode (int menuAlignmentMode)
Sets the current menuAlignmentMode
. Determines where the menu items in the BottomAppBar
will be aligned.
Parameters | |
---|---|
menuAlignmentMode |
int : the desired menuAlignmentMode, either MENU_ALIGNMENT_MODE_AUTO or MENU_ALIGNMENT_MODE_START .
|
setNavigationIcon
public void setNavigationIcon (Drawable drawable)
Parameters | |
---|---|
drawable |
Drawable |
setNavigationIconTint
public void setNavigationIconTint (int navigationIconTint)
Sets the color of the toolbar's navigation icon.
Parameters | |
---|---|
navigationIconTint |
int |
See also:
setSubtitle
public void setSubtitle (CharSequence subtitle)
Parameters | |
---|---|
subtitle |
CharSequence |
setTitle
public void setTitle (CharSequence title)
Parameters | |
---|---|
title |
CharSequence |
Protected methods
createFabDefaultXAnimation
protected void createFabDefaultXAnimation (int targetMode, List<Animator> animators)
Creates the default animation for moving a fab between alignment modes. Can be overridden by
extending classes to create a custom animation. Animations that should be executed should be
added to the animators list. The default animation defined here calls FloatingActionButton.hide()
and FloatingActionButton.show()
rather than using custom
animations.
Parameters | |
---|---|
targetMode |
int |
animators |
List |
getActionMenuViewTranslationX
protected int getActionMenuViewTranslationX (ActionMenuView actionMenuView, int fabAlignmentMode, boolean fabAttached)
Returns the X translation to position the ActionMenuView
. When fabAlignmentMode
is equal to FAB_ALIGNMENT_MODE_END
and fabAttached
is true, the ActionMenuView
will be aligned to the end of the navigation icon, otherwise the ActionMenuView
is not moved.
Parameters | |
---|---|
actionMenuView |
ActionMenuView |
fabAlignmentMode |
int |
fabAttached |
boolean |
Returns | |
---|---|
int |
onAttachedToWindow
protected void onAttachedToWindow ()
onLayout
protected void onLayout (boolean changed, int l, int t, int r, int b)
Parameters | |
---|---|
changed |
boolean |
l |
int |
t |
int |
r |
int |
b |
int |
onRestoreInstanceState
protected void onRestoreInstanceState (Parcelable state)
Parameters | |
---|---|
state |
Parcelable |
onSaveInstanceState
protected Parcelable onSaveInstanceState ()
Returns | |
---|---|
Parcelable |
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2023-12-15 UTC.