BottomNavigationView
public
class
BottomNavigationView
extends FrameLayout
java.lang.Object | ||||
↳ | android.view.View | |||
↳ | android.view.ViewGroup | |||
↳ | android.widget.FrameLayout | |||
↳ | com.google.android.material.bottomnavigation.BottomNavigationView |
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 HideBottomViewOnScrollBehavior
, 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 HideBottomViewOnScrollBehavior
.
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>
Summary
Nested classes | |
---|---|
interface |
BottomNavigationView.OnNavigationItemReselectedListener
Listener for handling reselection events on bottom navigation items. |
interface |
BottomNavigationView.OnNavigationItemSelectedListener
Listener for handling selection events on bottom navigation items. |
XML attributes | |
---|---|
BottomNavigationView_elevation |
|
BottomNavigationView_itemBackground |
|
BottomNavigationView_itemIconSize |
|
BottomNavigationView_itemIconTint |
|
BottomNavigationView_itemRippleColor |
|
BottomNavigationView_itemTextColor |
|
BottomNavigationView_labelVisibilityMode |
Inherited constants |
---|
![]()
android.view.ViewGroup
|
![]()
android.view.View
|
Inherited fields |
---|
![]()
android.view.View
|
Public constructors | |
---|---|
BottomNavigationView(Context context)
|
|
BottomNavigationView(Context context, AttributeSet attrs)
|
|
BottomNavigationView(Context context, AttributeSet attrs, int defStyleAttr)
|
Public methods | |
---|---|
BadgeDrawable
|
getBadge(int menuItemId)
Returns an instance of |
Drawable
|
getItemBackground()
Returns the background drawable of the menu items. |
int
|
getItemBackgroundResource()
This method is deprecated.
Use |
int
|
getItemIconSize()
Returns the size provided for the menu item icons in pixels. |
ColorStateList
|
getItemIconTintList()
Returns the tint which is applied to our menu items' icons. |
ColorStateList
|
getItemRippleColor()
Returns the color used to create a ripple as the background drawable of the menu items. |
int
|
getItemTextAppearanceActive()
Returns the text appearance used for the active menu item label. |
int
|
getItemTextAppearanceInactive()
Returns the text appearance used for inactive menu item labels. |
ColorStateList
|
getItemTextColor()
Returns colors used for the different states (normal, selected, focused, etc.) of the menu item text. |
int
|
getLabelVisibilityMode()
Returns the current label visibility mode used by this |
int
|
getMaxItemCount()
|
Menu
|
getMenu()
Returns the |
BadgeDrawable
|
getOrCreateBadge(int menuItemId)
Creates an instance of |
int
|
getSelectedItemId()
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
|
isItemHorizontalTranslationEnabled()
Returns whether the items horizontally translate on selection when the item widths fill up the screen. |
void
|
removeBadge(int menuItemId)
Removes the |
void
|
setElevation(float elevation)
Sets the base elevation of this view, in pixels. |
void
|
setItemBackground(Drawable background)
Set the background of our menu items to the given drawable. |
void
|
setItemBackgroundResource(int resId)
Set the background of our menu items to the given resource. |
void
|
setItemHorizontalTranslationEnabled(boolean itemHorizontalTranslationEnabled)
Sets whether the menu items horizontally translate on selection when the combined item widths fill up the screen. |
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
|
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
|
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
|
setLabelVisibilityMode(int labelVisibilityMode)
Sets the navigation items' label visibility mode. |
void
|
setOnNavigationItemReselectedListener(BottomNavigationView.OnNavigationItemReselectedListener listener)
Set a listener that will be notified when the currently selected bottom navigation item is reselected. |
void
|
setOnNavigationItemSelectedListener(BottomNavigationView.OnNavigationItemSelectedListener listener)
Set a listener that will be notified when a bottom navigation item is selected. |
void
|
setSelectedItemId(int itemId)
Set the selected menu item ID. |
Protected methods | |
---|---|
void
|
onAttachedToWindow()
|
void
|
onRestoreInstanceState(Parcelable state)
|
Parcelable
|
onSaveInstanceState()
|
XML attributes
BottomNavigationView_elevation
Related methods:
BottomNavigationView_itemBackground
Related methods:
BottomNavigationView_itemIconSize
Related methods:
BottomNavigationView_itemIconTint
Related methods:
BottomNavigationView_itemRippleColor
Related methods:
BottomNavigationView_itemTextColor
Related methods:
BottomNavigationView_labelVisibilityMode
Related methods:
Public constructors
BottomNavigationView
BottomNavigationView (Context context)
Parameters | |
---|---|
context |
Context |
BottomNavigationView
BottomNavigationView (Context context, AttributeSet attrs)
Parameters | |
---|---|
context |
Context |
attrs |
AttributeSet |
BottomNavigationView
BottomNavigationView (Context context, AttributeSet attrs, int defStyleAttr)
Parameters | |
---|---|
context |
Context |
attrs |
AttributeSet |
defStyleAttr |
int |
Public methods
getBadge
BadgeDrawable getBadge (int menuItemId)
Returns an instance of BadgeDrawable
associated with menuItemId
, null if none
was initialized.
Parameters | |
---|---|
menuItemId |
int : Id of the menu item. |
Returns | |
---|---|
BadgeDrawable |
an instance of BadgeDrawable associated with menuItemId or null. |
See also:
getItemBackground
Drawable getItemBackground ()
Returns the background drawable of the menu items.
Related XML Attributes:
Returns | |
---|---|
Drawable |
See also:
getItemBackgroundResource
int getItemBackgroundResource ()
This method is deprecated.
Use getItemBackground()
instead.
Returns the background resource of the menu items.
Related XML Attributes:
Returns | |
---|---|
int |
See also:
getItemIconSize
int getItemIconSize ()
Returns the size provided for the menu item icons in pixels.
Related XML Attributes:
Returns | |
---|---|
int |
See also:
getItemIconTintList
ColorStateList getItemIconTintList ()
Returns the tint which is applied to our menu items' icons.
Related XML Attributes:
Returns | |
---|---|
ColorStateList |
See also:
getItemRippleColor
ColorStateList getItemRippleColor ()
Returns the color used to create a ripple as the background drawable of the menu items. If a
background is set using setItemBackground(Drawable)
, this will return null.
Related XML Attributes:
Returns | |
---|---|
ColorStateList |
See also:
getItemTextAppearanceActive
int getItemTextAppearanceActive ()
Returns the text appearance used for the active menu item label.
Returns | |
---|---|
int |
the text appearance ID used for the active menu item label |
getItemTextAppearanceInactive
int getItemTextAppearanceInactive ()
Returns the text appearance used for inactive menu item labels.
Returns | |
---|---|
int |
the text appearance ID used for inactive menu item labels |
getItemTextColor
ColorStateList getItemTextColor ()
Returns colors used for the different states (normal, selected, focused, etc.) of the menu item text.
Related XML Attributes:
Returns | |
---|---|
ColorStateList |
the ColorStateList of colors used for the different states of the menu items text. |
See also:
getLabelVisibilityMode
int getLabelVisibilityMode ()
Returns the current label visibility mode used by this BottomNavigationView
.
Related XML Attributes:
Returns | |
---|---|
int |
See also:
getMaxItemCount
int getMaxItemCount ()
Returns | |
---|---|
int |
The maximum number of items that can be shown in BottomNavigationView. |
getMenu
Menu getMenu ()
Returns the Menu
instance associated with this bottom navigation bar.
Returns | |
---|---|
Menu |
getOrCreateBadge
BadgeDrawable getOrCreateBadge (int menuItemId)
Creates an instance of BadgeDrawable
associated with menuItemId
if none exists.
Initializes (if needed) and returns the associated instance of BadgeDrawable
associated
with menuItemId
.
Parameters | |
---|---|
menuItemId |
int : Id of the menu item. |
Returns | |
---|---|
BadgeDrawable |
an instance of BadgeDrawable associated with menuItemId .
|
getSelectedItemId
int getSelectedItemId ()
Returns the currently selected menu item ID, or zero if there is no menu.
Returns | |
---|---|
int |
See also:
inflateMenu
void inflateMenu (int resId)
Inflate a menu resource into this navigation view.
Existing items in the menu will not be modified or removed.
Parameters | |
---|---|
resId |
int : ID of a menu resource to inflate
|
isItemHorizontalTranslationEnabled
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 |
removeBadge
void removeBadge (int menuItemId)
Removes the BadgeDrawable
associated with menuItemId
. Do nothing if none
exists. Consider changing the visibility of the BadgeDrawable
if you only want to hide
it temporarily.
Parameters | |
---|---|
menuItemId |
int : Id of the menu item.
|
setElevation
void setElevation (float elevation)
Sets the base elevation of this view, in pixels.
Related XML Attributes:
Parameters | |
---|---|
elevation |
float |
setItemBackground
void setItemBackground (Drawable background)
Set the background of our menu items to the given drawable.
This will remove any ripple backgrounds created by setItemRippleColor(ColorStateList)
.
Related XML Attributes:
Parameters | |
---|---|
background |
Drawable : The drawable for the background. |
setItemBackgroundResource
void setItemBackgroundResource (int resId)
Set the background of our menu items to the given resource.
This will remove any ripple backgrounds created by setItemRippleColor(ColorStateList)
.
Related XML Attributes:
Parameters | |
---|---|
resId |
int : The identifier of the resource. |
setItemHorizontalTranslationEnabled
void setItemHorizontalTranslationEnabled (boolean itemHorizontalTranslationEnabled)
Sets whether the menu items horizontally translate on selection when the combined item widths fill up the screen.
Parameters | |
---|---|
itemHorizontalTranslationEnabled |
boolean : whether the items horizontally translate on selection |
See also:
setItemIconSize
void setItemIconSize (int iconSize)
Set the size to provide for the menu item icons.
For best image resolution, use an icon with the same size set in this method.
Related XML Attributes:
Parameters | |
---|---|
iconSize |
int : the size in pixels to provide for the menu item icons |
setItemIconSizeRes
void setItemIconSizeRes (int iconSizeRes)
Set the size to provide for the menu item icons using a resource ID.
For best image resolution, use an icon with the same size set in this method.
Related XML Attributes:
Parameters | |
---|---|
iconSizeRes |
int : the resource ID for the size to provide for the menu item icons |
setItemIconTintList
void setItemIconTintList (ColorStateList tint)
Set the tint which is applied to our menu items' icons.
Related XML Attributes:
Parameters | |
---|---|
tint |
ColorStateList : the tint to apply. |
setItemRippleColor
void setItemRippleColor (ColorStateList itemRippleColor)
Set the background of our menu items to be a ripple with the given colors.
Related XML Attributes:
Parameters | |
---|---|
itemRippleColor |
ColorStateList : The ColorStateList for the ripple. This will create a ripple
background for menu items, replacing any background previously set by setItemBackground(Drawable) . |
setItemTextAppearanceActive
void setItemTextAppearanceActive (int textAppearanceRes)
Sets the text appearance to be used for the menu item labels.
Parameters | |
---|---|
textAppearanceRes |
int : the text appearance ID used for menu item labels
|
setItemTextAppearanceInactive
void setItemTextAppearanceInactive (int textAppearanceRes)
Sets the text appearance to be used for inactive menu item labels.
Parameters | |
---|---|
textAppearanceRes |
int : the text appearance ID used for inactive menu item labels
|
setItemTextColor
void setItemTextColor (ColorStateList textColor)
Set the colors to use for the different states (normal, selected, focused, etc.) of the menu item text.
Related XML Attributes:
Parameters | |
---|---|
textColor |
ColorStateList |
See also:
setLabelVisibilityMode
void setLabelVisibilityMode (int labelVisibilityMode)
Sets the navigation items' label visibility mode.
The label is either always shown, never shown, or only shown when activated. Also supports "auto" mode, which uses the item count to determine whether to show or hide the label.
Related XML Attributes:
Parameters | |
---|---|
labelVisibilityMode |
int : mode which decides whether or not the label should be shown. Can be
one of LABEL_VISIBILITY_AUTO , LABEL_VISIBILITY_SELECTED , LABEL_VISIBILITY_LABELED , or LABEL_VISIBILITY_UNLABELED |
See also:
setOnNavigationItemReselectedListener
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 BottomNavigationView.OnNavigationItemSelectedListener
to be set.
Parameters | |
---|---|
listener |
BottomNavigationView.OnNavigationItemReselectedListener : The listener to notify |
setOnNavigationItemSelectedListener
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 BottomNavigationView.OnNavigationItemReselectedListener
has also been set.
Parameters | |
---|---|
listener |
BottomNavigationView.OnNavigationItemSelectedListener : The listener to notify |
setSelectedItemId
void setSelectedItemId (int itemId)
Set the selected menu item ID. This behaves the same as tapping on an item.
Parameters | |
---|---|
itemId |
int : The menu item ID. If no item has this ID, the current selection is unchanged. |
See also:
Protected methods
onAttachedToWindow
void onAttachedToWindow ()
onRestoreInstanceState
void onRestoreInstanceState (Parcelable state)
Parameters | |
---|---|
state |
Parcelable |
onSaveInstanceState
Parcelable onSaveInstanceState ()
Returns | |
---|---|
Parcelable |
Content and code samples on this page are subject to the licenses described in the Content License. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2020-07-20 UTC.