BottomNavigationView

public class BottomNavigationView extends NavigationBarView


Represents a standard bottom navigation bar for application. It is an implementation of material design bottom navigation.

Bottom navigation bars make it easy for users to explore and switch between top-level views in a single tap. They should be used when an application has three to five top-level destinations.

The bar can disappear on scroll, based on , when it is placed within a CoordinatorLayout and one of the children within the CoordinatorLayout is scrolled. This behavior is only set if the layout_behavior property is set to .

The bar contents can be populated by specifying a menu resource file. Each menu item title, icon and enabled state will be used for displaying bottom navigation bar items. Menu items can also be used for programmatically selecting which destination is currently active. It can be done using MenuItem#setChecked(true)

layout resource file:
<com.google.android.material.bottomnavigation.BottomNavigationView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schema.android.com/apk/res/res-auto"
    android:id="@+id/navigation"
    android:layout_width="match_parent"
    android:layout_height="56dp"
    android:layout_gravity="start"
    app:menu="@menu/my_navigation_items" />

res/menu/my_navigation_items.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/action_search"
         android:title="@string/menu_search"
         android:icon="@drawable/ic_search" />
    <item android:id="@+id/action_settings"
         android:title="@string/menu_settings"
         android:icon="@drawable/ic_add" />
    <item android:id="@+id/action_navigation"
         android:title="@string/menu_navigation"
         android:icon="@drawable/ic_action_navigation_menu" />
</menu>

For more information, see the component developer guidance and design guidelines.

Summary

Public constructors

BottomNavigationView(Context context)
BottomNavigationView(Context context, AttributeSet attrs)
BottomNavigationView(
    Context context,
    AttributeSet attrs,
    int defStyleAttr
)
BottomNavigationView(
    Context context,
    AttributeSet attrs,
    int defStyleAttr,
    int defStyleRes
)

Public methods

int

Returns the maximum number of items that can be shown in NavigationBarView.

boolean

Returns whether the items horizontally translate on selection when the item widths fill up the screen.

boolean
onTouchEvent(MotionEvent event)
void
setItemHorizontalTranslationEnabled(
    boolean itemHorizontalTranslationEnabled
)

Sets whether the menu items horizontally translate on selection when the combined item widths fill up the screen.

void

This method is deprecated.

Use setOnItemReselectedListener instead.

void

This method is deprecated.

Use setOnItemSelectedListener instead.

Protected methods

void
onMeasure(int widthMeasureSpec, int heightMeasureSpec)

Inherited Constants

From com.google.android.material.navigation.NavigationBarView
static final int

The active indicator width fills up the width of its parent.

static final int

The active indicator width wraps the content.

static final int

Item is placed at the center of its container

static final int

Item is placed at the start center of its container

static final int

Item is placed at the top center of its container

static final int

Icon is placed at the top of the item

static final int

Icon is placed at the top of the item

static final int

Label behaves as "labeled" when there are 3 items or less, or "selected" when there are 4 items or more.

static final int

Label is shown on all navigation items.

static final int

Label is shown on the selected navigation item.

static final int

Label is not shown on any navigation items.

Inherited methods

From com.google.android.material.navigation.NavigationBarView
int

Get the distance between the active indicator container and the item's label.

BadgeDrawable
getBadge(int menuItemId)

Returns an instance of BadgeDrawable associated with menuItemId, null if none was initialized.

int

Returns the text appearance used for the active menu item label when they are in the horizontal item layout (when the start icon value is ITEM_ICON_GRAVITY_START).

int

Returns the text appearance used for inactive menu item labels when they are in the horizontal item layout (when the start icon value is ITEM_ICON_GRAVITY_START).

int

Get the horizontal distance between the icon and the item's label when the item is in the ITEM_ICON_GRAVITY_START configuration.

ColorStateList

Get the color of the active indicator drawable.

int

Get the height of an item's active indicator when it is expanded to wrap the item content, ie. when it is in the ITEM_ICON_GRAVITY_START configuration.

int

Get the margin that will be maintained at the start and end of the expanded active indicator away from the edges of its parent container.

int

Get the width of an item's active indicator when it is expanded to wrap the item content, ie. when it is in the ITEM_ICON_GRAVITY_START configuration.

int

Get the width of an item's active indicator.

int

Get the margin that will be maintained at the start and end of the active indicator away from the edges of its parent container.

ShapeAppearanceModel

Get the ShapeAppearanceModel of the active indicator drawable.

int

Get the width of an item's active indicator.

Drawable

Returns the background drawable of the menu items.

int

This method is deprecated.

Use getItemBackground instead.

int

Returns the navigation items' layout gravity.

int

Returns the current item icon gravity.

int

Returns the size provided for the menu item icons in pixels.

ColorStateList

Returns the tint which is applied to our menu items' icons.

int

Get the distance from the bottom of an item's label to the bottom of the navigation bar item.

int

Get the distance from the top of an item's icon/active indicator to the top of the navigation bar item.

ColorStateList

Returns the color used to create a ripple as the background drawable of the menu items.

int

Returns the text appearance used for the active menu item label.

int

Returns the text appearance used for inactive menu item labels.

ColorStateList

Returns colors used for the different states (normal, selected, focused, etc.) of the menu item text.

int
getLabelMaxLines(int labelMaxLines)

Returns the max lines limit for the label text.

int

Returns the current label visibility mode used by this NavigationBarView.

Menu

Returns the Menu instance associated with this navigation bar.

ViewGroup

Returns the android.view.ViewGroup associated with the navigation bar menu.

BadgeDrawable
getOrCreateBadge(int menuItemId)

Creates an instance of BadgeDrawable associated with menuItemId if none exists.

boolean

Returns whether or not the label text should scale with the system font size.

int

Returns the currently selected menu item ID, or zero if there is no menu.

void
inflateMenu(int resId)

Inflate a menu resource into this navigation view.

boolean

Get whether or not a selected item should show an active indicator.

boolean

Returns whether or not submenus are supported.

void
void
onRestoreInstanceState(Parcelable state)
Parcelable
void
removeBadge(int menuItemId)

Removes the BadgeDrawable associated with menuItemId.

void
setActiveIndicatorLabelPadding(int activeIndicatorLabelPadding)

Set the distance between the active indicator container and the item's label.

void
setElevation(float elevation)

Sets the base elevation of this view, in pixels.

void

Sets the text appearance to be used for the menu item labels when they are in the horizontal item layout (when the start icon value is ITEM_ICON_GRAVITY_START).

void

Sets the text appearance to be used for inactive menu item labels when they are in the horizontal item layout (when the start icon value is ITEM_ICON_GRAVITY_START).

void
setIconLabelHorizontalSpacing(int iconLabelSpacing)

Set the horizontal distance between the icon and the item's label when the item is in the ITEM_ICON_GRAVITY_START configuration.

void
setItemActiveIndicatorColor(ColorStateList csl)

Set the ColorStateList of the active indicator drawable.

void

Set whether a selected item should show an active indicator.

void

Set the height of an item's active indicator when it is expanded to wrap the item content, ie. when it is in the ITEM_ICON_GRAVITY_START configuration.

void

Set the horizontal margin that will be maintained at the start and end of the expanded active indicator, making sure the indicator maintains the given distance from the edge of its parent container.

void
setItemActiveIndicatorExpandedPadding(
    int paddingLeft,
    int paddingTop,
    int paddingRight,
    int paddingBottom
)

Set the padding of the expanded active indicator wrapping the content.

void

Set the width of an item's active indicator when it is expanded to wrap the item content, ie. when it is in the ITEM_ICON_GRAVITY_START configuration.

void

Set the height of an item's active indicator.

void

Set the horizontal margin that will be maintained at the start and end of the active indicator, making sure the indicator remains the given distance from the edge of its parent container.

void

Set the ShapeAppearanceModel of the active indicator drawable.

void

Set the width of an item's active indicator.

void
setItemBackground(Drawable background)

Set the background of our menu items to the given drawable.

void

Set the background of our menu items to the given resource.

void
setItemGravity(int itemGravity)

Sets the navigation items' layout gravity.

void
setItemIconGravity(int itemIconGravity)

Sets the navigation items' icon gravity.

void
setItemIconSize(int iconSize)

Set the size to provide for the menu item icons.

void
setItemIconSizeRes(int iconSizeRes)

Set the size to provide for the menu item icons using a resource ID.

void
setItemIconTintList(ColorStateList tint)

Set the tint which is applied to our menu items' icons.

void
setItemOnTouchListener(int menuItemId, OnTouchListener onTouchListener)

Sets an android.view.View.OnTouchListener for the item view associated with the provided menuItemId.

void
setItemPaddingBottom(int paddingBottom)

Set the distance from the bottom of an item's label to the bottom of the navigation bar item.

void
setItemPaddingTop(int paddingTop)

Set the distance from the top of an items icon/active indicator to the top of the navigation bar item.

void
setItemRippleColor(ColorStateList itemRippleColor)

Set the background of our menu items to be a ripple with the given colors.

void
setItemTextAppearanceActive(int textAppearanceRes)

Sets the text appearance to be used for the menu item labels.

void

Sets whether the active menu item labels are bold.

void
setItemTextAppearanceInactive(int textAppearanceRes)

Sets the text appearance to be used for inactive menu item labels.

void
setItemTextColor(ColorStateList textColor)

Set the colors to use for the different states (normal, selected, focused, etc.) of the menu item text.

void
setLabelFontScalingEnabled(boolean labelFontScalingEnabled)

Sets whether or not the label text should scale with the system font size.

void
setLabelMaxLines(int labelMaxLines)

Set the max lines limit for the label text.

void
setLabelVisibilityMode(int labelVisibilityMode)

Sets the navigation items' label visibility mode.

void

Set a listener that will be notified when the currently selected navigation item is reselected.

void

Set a listener that will be notified when a navigation item is selected.

void
setSelectedItemId(int itemId)

Set the selected menu item ID.

Public constructors

BottomNavigationView

public BottomNavigationView(Context context)

BottomNavigationView

public BottomNavigationView(Context context, AttributeSet attrs)

BottomNavigationView

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

BottomNavigationView

public BottomNavigationView(
    Context context,
    AttributeSet attrs,
    int defStyleAttr,
    int defStyleRes
)

Public methods

getMaxItemCount

public int getMaxItemCount()

Returns the maximum number of items that can be shown in NavigationBarView.

isItemHorizontalTranslationEnabled

public boolean isItemHorizontalTranslationEnabled()

Returns whether the items horizontally translate on selection when the item widths fill up the screen.

Returns
boolean

whether the menu items horizontally translate on selection

onTouchEvent

public boolean onTouchEvent(MotionEvent event)

setItemHorizontalTranslationEnabled

public void setItemHorizontalTranslationEnabled(
    boolean itemHorizontalTranslationEnabled
)

Sets whether the menu items horizontally translate on selection when the combined item widths fill up the screen.

Parameters
boolean itemHorizontalTranslationEnabled

whether the items horizontally translate on selection

setOnNavigationItemReselectedListener

public void setOnNavigationItemReselectedListener(
    BottomNavigationView.OnNavigationItemReselectedListener listener
)

Set a listener that will be notified when the currently selected bottom navigation item is reselected. This does not require an OnNavigationItemSelectedListener to be set.

Parameters
BottomNavigationView.OnNavigationItemReselectedListener listener

The listener to notify

setOnNavigationItemSelectedListener

public void setOnNavigationItemSelectedListener(
    BottomNavigationView.OnNavigationItemSelectedListener listener
)

Set a listener that will be notified when a bottom navigation item is selected. This listener will also be notified when the currently selected item is reselected, unless an has also been set.

Parameters
BottomNavigationView.OnNavigationItemSelectedListener listener

The listener to notify

Protected methods

onMeasure

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)