BottomAppBar

public class 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 setAnchorId, or by setting app:layout_anchor on the FAB in xml.

Note: The Material Design Guidelines caution against using an 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 . 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.

ref com.google.android.material.R.styleable#BottomAppBar_backgroundTint

ref com.google.android.material.R.styleable#BottomAppBar_fabAlignmentMode

ref com.google.android.material.R.styleable#BottomAppBar_fabAnchorMode

ref com.google.android.material.R.styleable#BottomAppBar_fabAnimationMode

ref com.google.android.material.R.styleable#BottomAppBar_fabCradleMargin

ref com.google.android.material.R.styleable#BottomAppBar_fabCradleRoundedCornerRadius

ref com.google.android.material.R.styleable#BottomAppBar_fabCradleVerticalOffset

ref com.google.android.material.R.styleable#BottomAppBar_hideOnScroll

ref com.google.android.material.R.styleable#BottomAppBar_paddingBottomSystemWindowInsets

Summary

Nested types

Behavior designed for use with BottomAppBar instances.

@Retention(value = RetentionPolicy.SOURCE)
public annotation BottomAppBar.FabAlignmentMode

The fabAlignmentMode determines the horizontal positioning of the cradle and the FAB which can be centered or aligned to the end.

@Retention(value = RetentionPolicy.SOURCE)
public annotation BottomAppBar.FabAnimationMode

The fabAnimationMode determines the animation used to move the FAB between different alignment modes.

Constants

static final int
static final int
static final int

The FAB is cradled at the top of the BottomAppBar.

static final int

The FAB is embedded inside the BottomAppBar.

static final int
static final int
static final int

The menu items are aligned automatically to avoid the FAB.

static final int

The menu items are aligned to the start.

Public constructors

BottomAppBar(Context context)
BottomAppBar(Context context, AttributeSet attrs)
BottomAppBar(Context context, AttributeSet attrs, int defStyleAttr)

Public methods

void

Add a listener that will be called when the bottom app bar scroll state changes.

void

Remove all previously added OnScrollStateChangedListeners.

ColorStateList
BottomAppBar.Behavior
float

Returns the vertical offset for the fab cutout.

int

Returns the current fabAlignmentMode, either FAB_ALIGNMENT_MODE_CENTER or FAB_ALIGNMENT_MODE_END.

int

Returns the FloatingActionButton end margin pixel offset for the fab if it is set.

int

Returns the current fabAnchorMode, either FAB_ANCHOR_MODE_CRADLE or FAB_ANCHOR_MODE_EMBED.

int

Returns the current fabAnimationMode, either FAB_ANIMATION_MODE_SCALE or FAB_ANIMATION_MODE_SLIDE.

float

Returns the cradle margin for the fab cutout.

float

Returns the rounded corner radius for the cutout if it exists.

boolean

Returns true if the BottomAppBar should hide when a is scrolled.

int

Returns the current menuAlignmentMode, either MENU_ALIGNMENT_MODE_AUTO or MENU_ALIGNMENT_MODE_START.

boolean

Returns true if the BottomAppBar is scrolled down.

boolean

Returns true if the BottomAppBar is scrolled up.

void

Animates the BottomAppBar so it hides off the screen.

void
performHide(boolean animate)

Hides the BottomAppBar.

void

Animates the BottomAppBar so it is shown on the screen.

void
performShow(boolean animate)

Shows the BottomAppBar.

void

Remove a listener that was previously added via addOnScrollStateChangedListener.

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 FloatingActionButton being cradled.

void
setElevation(float elevation)
void

Sets the current fabAlignmentMode.

void
setFabAlignmentModeAndReplaceMenu(
    @BottomAppBar.FabAlignmentMode int fabAlignmentMode,
    int newMenu
)

Sets the current fabAlignmentMode and replaces the BottomAppBar's menu resource.

void

Sets the end margin, in pixels, of the FloatingActionButton.

void
setFabAnchorMode(int fabAnchorMode)

Sets the current fabAnchorMode.

void

Sets the current fabAnimationMode.

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 BottomAppBar should hide when a is scrolled.

void
setMenuAlignmentMode(int menuAlignmentMode)

Sets the current menuAlignmentMode.

void
setNavigationIcon(Drawable drawable)
void
setNavigationIconTint(int navigationIconTint)

Sets the color of the toolbar's navigation icon.

void
void

Protected methods

void
createFabDefaultXAnimation(
    @BottomAppBar.FabAlignmentMode int targetMode,
    List<Animator> animators
)

Creates the default animation for moving a fab between alignment modes.

int
getActionMenuViewTranslationX(
    ActionMenuView actionMenuView,
    @BottomAppBar.FabAlignmentMode int fabAlignmentMode,
    boolean fabAttached
)

Returns the X translation to position the ActionMenuView.

void
void
onLayout(boolean changed, int l, int t, int r, int b)
void
onRestoreInstanceState(Parcelable state)
Parcelable

Constants

FAB_ALIGNMENT_MODE_CENTER

public static final int FAB_ALIGNMENT_MODE_CENTER = 0

FAB_ALIGNMENT_MODE_END

public static final int FAB_ALIGNMENT_MODE_END = 1

FAB_ANCHOR_MODE_CRADLE

public static final int FAB_ANCHOR_MODE_CRADLE = 1

The FAB is cradled at the top of the BottomAppBar.

FAB_ANCHOR_MODE_EMBED

public static final int FAB_ANCHOR_MODE_EMBED = 0

The FAB is embedded inside the BottomAppBar.

FAB_ANIMATION_MODE_SCALE

public static final int FAB_ANIMATION_MODE_SCALE = 0

FAB_ANIMATION_MODE_SLIDE

public static final int FAB_ANIMATION_MODE_SLIDE = 1
public static final int MENU_ALIGNMENT_MODE_AUTO = 0

The menu items are aligned automatically to avoid the FAB.

public static final int MENU_ALIGNMENT_MODE_START = 1

The menu items are aligned to the start.

Public fields

behavior

public BottomAppBar.Behavior behavior

fabAlignmentMode

@BottomAppBar.FabAlignmentMode
public int fabAlignmentMode

fabAlignmentModeEndMargin

public int fabAlignmentModeEndMargin

fabAnchorMode

public int fabAnchorMode

fabAnimationMode

@BottomAppBar.FabAnimationMode
public int fabAnimationMode

hideOnScroll

public boolean hideOnScroll
public int menuAlignmentMode

Public constructors

BottomAppBar

public BottomAppBar(Context context)

BottomAppBar

public BottomAppBar(Context context, AttributeSet attrs)

BottomAppBar

public BottomAppBar(Context context, AttributeSet attrs, int defStyleAttr)

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 OnScrollStateChangedListener.

Components that add a listener should take care to remove it when finished via removeOnScrollStateChangedListener.

clearOnScrollStateChangedListeners

public void clearOnScrollStateChangedListeners()

Remove all previously added OnScrollStateChangedListeners.

getBackgroundTint

public ColorStateList getBackgroundTint()

getBehavior

public BottomAppBar.Behavior getBehavior()

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.

getFabAlignmentMode

@BottomAppBar.FabAlignmentMode
public int getFabAlignmentMode()

Returns the current fabAlignmentMode, either FAB_ALIGNMENT_MODE_CENTER or FAB_ALIGNMENT_MODE_END.

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.

getFabAnchorMode

public int getFabAnchorMode()

Returns the current fabAnchorMode, either FAB_ANCHOR_MODE_CRADLE or FAB_ANCHOR_MODE_EMBED.

getFabAnimationMode

@BottomAppBar.FabAnimationMode
public int getFabAnimationMode()

Returns the current fabAnimationMode, either FAB_ANIMATION_MODE_SCALE or FAB_ANIMATION_MODE_SLIDE.

getFabCradleMargin

public float getFabCradleMargin()

Returns the cradle margin for the fab cutout. This is the space between the fab and the cutout.

getFabCradleRoundedCornerRadius

public float getFabCradleRoundedCornerRadius()

Returns the rounded corner radius for the cutout if it exists. A value of 0 will be a sharp edge.

getHideOnScroll

public boolean getHideOnScroll()

Returns true if the BottomAppBar should hide when a is scrolled. This is handled by .

getMenuAlignmentMode

public int getMenuAlignmentMode()

Returns the current menuAlignmentMode, either MENU_ALIGNMENT_MODE_AUTO or MENU_ALIGNMENT_MODE_START.

isScrolledDown

public boolean isScrolledDown()

Returns true if the BottomAppBar is scrolled down.

isScrolledUp

public boolean isScrolledUp()

Returns true if the BottomAppBar is scrolled up.

performHide

public void performHide()

Animates the BottomAppBar so it hides off the screen.

performHide

public void performHide(boolean animate)

Hides the BottomAppBar.

Parameters
boolean animate

false to hide the BottomAppBar immediately without animation.

performShow

public void performShow()

Animates the BottomAppBar so it is shown on the screen.

performShow

public void performShow(boolean animate)

Shows the BottomAppBar.

Parameters
boolean animate

false to show the BottomAppBar immediately without animation.

removeOnScrollStateChangedListener

public void removeOnScrollStateChangedListener(
    HideBottomViewOnScrollBehavior.OnScrollStateChangedListener listener
)

Remove a listener that was previously added via addOnScrollStateChangedListener.

Parameters
HideBottomViewOnScrollBehavior.OnScrollStateChangedListener listener

listener to remove

replaceMenu

public void replaceMenu(int newMenu)

A convenience method to replace the contents of the BottomAppBar's menu.

Parameters
int newMenu

the desired new menu.

setBackgroundTint

public void setBackgroundTint(ColorStateList backgroundTint)

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.

setElevation

public void setElevation(float elevation)

setFabAlignmentMode

public void setFabAlignmentMode(@BottomAppBar.FabAlignmentMode int fabAlignmentMode)

Sets the current fabAlignmentMode. An animated transition between current and desired modes will be played.

Parameters
@BottomAppBar.FabAlignmentMode int fabAlignmentMode

the desired fabAlignmentMode, either FAB_ALIGNMENT_MODE_CENTER or FAB_ALIGNMENT_MODE_END.

setFabAlignmentModeAndReplaceMenu

public void setFabAlignmentModeAndReplaceMenu(
    @BottomAppBar.FabAlignmentMode 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
@BottomAppBar.FabAlignmentMode int fabAlignmentMode

the desired fabAlignmentMode, either FAB_ALIGNMENT_MODE_CENTER or FAB_ALIGNMENT_MODE_END.

int newMenu

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.

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.

setFabAnchorMode

public void setFabAnchorMode(int fabAnchorMode)

Sets the current fabAnchorMode.

Parameters
int fabAnchorMode

the desired fabAnchorMode, either FAB_ANCHOR_MODE_CRADLE or FAB_ANCHOR_MODE_EMBED.

setFabAnimationMode

public void setFabAnimationMode(@BottomAppBar.FabAnimationMode int fabAnimationMode)

Sets the current fabAnimationMode.

Parameters
@BottomAppBar.FabAnimationMode int fabAnimationMode

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.

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.

setHideOnScroll

public void setHideOnScroll(boolean hide)

Sets if the BottomAppBar should hide when a is scrolled. This is handled by .

setMenuAlignmentMode

public void setMenuAlignmentMode(int menuAlignmentMode)

Sets the current menuAlignmentMode. Determines where the menu items in the BottomAppBar will be aligned.

Parameters
int menuAlignmentMode

the desired menuAlignmentMode, either MENU_ALIGNMENT_MODE_AUTO or MENU_ALIGNMENT_MODE_START.

setNavigationIcon

public void setNavigationIcon(Drawable drawable)

setNavigationIconTint

public void setNavigationIconTint(int navigationIconTint)

Sets the color of the toolbar's navigation icon.

setSubtitle

public void setSubtitle(CharSequence subtitle)

setTitle

public void setTitle(CharSequence title)

Protected methods

createFabDefaultXAnimation

protected void createFabDefaultXAnimation(
    @BottomAppBar.FabAlignmentMode 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 hide and show rather than using custom animations.

getActionMenuViewTranslationX

protected int getActionMenuViewTranslationX(
    ActionMenuView actionMenuView,
    @BottomAppBar.FabAlignmentMode 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 will be aligned to the end of the navigation icon, otherwise the is not moved.

onAttachedToWindow

protected void onAttachedToWindow()

onLayout

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

onRestoreInstanceState

protected void onRestoreInstanceState(Parcelable state)

onSaveInstanceState

protected Parcelable onSaveInstanceState()