NavigationBarView
public
abstract
class
NavigationBarView
extends FrameLayout
java.lang.Object | ||||
↳ | android.view.View | |||
↳ | android.view.ViewGroup | |||
↳ | android.widget.FrameLayout | |||
↳ | com.google.android.material.navigation.NavigationBarView |
Provides an abstract implementation of a navigation bar that can be used to implementation such as Bottom Navigation or Navigation rail.
Navigation bars make it easy for users to explore and switch between top-level views in a single tap.
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 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)
Summary
Nested classes | |
---|---|
interface |
NavigationBarView.OnItemReselectedListener
Listener for handling reselection events on navigation items. |
interface |
NavigationBarView.OnItemSelectedListener
Listener for handling selection events on navigation items. |
XML attributes | |
---|---|
NavigationBarView_labelVisibilityMode |
Constants | |
---|---|
int |
LABEL_VISIBILITY_AUTO
Label behaves as "labeled" when there are 3 items or less, or "selected" when there are 4 items or more. |
int |
LABEL_VISIBILITY_LABELED
Label is shown on all navigation items. |
int |
LABEL_VISIBILITY_SELECTED
Label is shown on the selected navigation item. |
int |
LABEL_VISIBILITY_UNLABELED
Label is not shown on any navigation items. |
Inherited constants |
---|
Inherited fields |
---|
Public constructors | |
---|---|
NavigationBarView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
|
Public methods | |
---|---|
int
|
getActiveIndicatorLabelPadding()
Get the distance between the active indicator container and the item's label. |
BadgeDrawable
|
getBadge(int menuItemId)
Returns an instance of |
ColorStateList
|
getItemActiveIndicatorColor()
Get the color of the active indicator drawable. |
int
|
getItemActiveIndicatorHeight()
Get the width of an item's active indicator. |
int
|
getItemActiveIndicatorMarginHorizontal()
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
|
getItemActiveIndicatorShapeAppearance()
Get the |
int
|
getItemActiveIndicatorWidth()
Get the width of an item's active indicator. |
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. |
int
|
getItemPaddingBottom()
Get the distance from the bottom of an item's label to the bottom of the navigation bar item. |
int
|
getItemPaddingTop()
Get the distance from the top of an item's icon/active indicator to the top of the navigation bar item. |
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 |
abstract
int
|
getMaxItemCount()
Returns the maximum number of items that can be shown in NavigationBarView. |
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
|
isItemActiveIndicatorEnabled()
Get whether or not a selected item should show an active indicator. |
void
|
removeBadge(int menuItemId)
Removes the |
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
|
setItemActiveIndicatorColor(ColorStateList csl)
Set the |
void
|
setItemActiveIndicatorEnabled(boolean enabled)
Set whether a selected item should show an active indicator. |
void
|
setItemActiveIndicatorHeight(int height)
Set the height of an item's active indicator. |
void
|
setItemActiveIndicatorMarginHorizontal(int horizontalMargin)
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
|
setItemActiveIndicatorShapeAppearance(ShapeAppearanceModel shapeAppearance)
Set the |
void
|
setItemActiveIndicatorWidth(int width)
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
|
setItemBackgroundResource(int resId)
Set the background of our menu items to the given resource. |
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, View.OnTouchListener onTouchListener)
Sets an |
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
|
setItemTextAppearanceActiveBoldEnabled(boolean isBold)
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
|
setLabelVisibilityMode(int labelVisibilityMode)
Sets the navigation items' label visibility mode. |
void
|
setOnItemReselectedListener(NavigationBarView.OnItemReselectedListener listener)
Set a listener that will be notified when the currently selected navigation item is reselected. |
void
|
setOnItemSelectedListener(NavigationBarView.OnItemSelectedListener listener)
Set a listener that will be notified when a navigation item is selected. |
void
|
setSelectedItemId(int itemId)
Set the selected menu item ID. |
Protected methods | |
---|---|
void
|
onAttachedToWindow()
|
void
|
onRestoreInstanceState(Parcelable state)
|
Parcelable
|
onSaveInstanceState()
|
Inherited methods | |
---|---|
XML attributes
NavigationBarView_labelVisibilityMode
Related methods:
Constants
LABEL_VISIBILITY_AUTO
public static final int LABEL_VISIBILITY_AUTO
Label behaves as "labeled" when there are 3 items or less, or "selected" when there are 4 items or more.
Constant Value: -1 (0xffffffff)
LABEL_VISIBILITY_LABELED
public static final int LABEL_VISIBILITY_LABELED
Label is shown on all navigation items.
Constant Value: 1 (0x00000001)
LABEL_VISIBILITY_SELECTED
public static final int LABEL_VISIBILITY_SELECTED
Label is shown on the selected navigation item.
Constant Value: 0 (0x00000000)
LABEL_VISIBILITY_UNLABELED
public static final int LABEL_VISIBILITY_UNLABELED
Label is not shown on any navigation items.
Constant Value: 2 (0x00000002)
Public constructors
NavigationBarView
public NavigationBarView (Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
Parameters | |
---|---|
context |
Context |
attrs |
AttributeSet |
defStyleAttr |
int |
defStyleRes |
int |
Public methods
getActiveIndicatorLabelPadding
public int getActiveIndicatorLabelPadding ()
Get the distance between the active indicator container and the item's label.
Returns | |
---|---|
int |
getBadge
public 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:
getItemActiveIndicatorColor
public ColorStateList getItemActiveIndicatorColor ()
Get the color of the active indicator drawable.
Returns | |
---|---|
ColorStateList |
A ColorStateList used as the color of the active indicator.
|
getItemActiveIndicatorHeight
public int getItemActiveIndicatorHeight ()
Get the width of an item's active indicator.
Returns | |
---|---|
int |
The width, in pixels, of a menu item's active indicator. |
getItemActiveIndicatorMarginHorizontal
public int getItemActiveIndicatorMarginHorizontal ()
Get the margin that will be maintained at the start and end of the active indicator away from the edges of its parent container.
Returns | |
---|---|
int |
The horizontal margin, in pixels. |
getItemActiveIndicatorShapeAppearance
public ShapeAppearanceModel getItemActiveIndicatorShapeAppearance ()
Get the ShapeAppearanceModel
of the active indicator drawable.
Returns | |
---|---|
ShapeAppearanceModel |
The ShapeAppearanceModel of the active indicator drawable.
|
getItemActiveIndicatorWidth
public int getItemActiveIndicatorWidth ()
Get the width of an item's active indicator.
Returns | |
---|---|
int |
The width, in pixels, of a menu item's active indicator. |
getItemBackground
public Drawable getItemBackground ()
Returns the background drawable of the menu items.
Returns | |
---|---|
Drawable |
See also:
getItemBackgroundResource
public int getItemBackgroundResource ()
This method is deprecated.
Use getItemBackground()
instead.
Returns the background resource of the menu items.
Returns | |
---|---|
int |
See also:
getItemIconSize
public int getItemIconSize ()
Returns the size provided for the menu item icons in pixels.
Returns | |
---|---|
int |
See also:
getItemIconTintList
public ColorStateList getItemIconTintList ()
Returns the tint which is applied to our menu items' icons.
Returns | |
---|---|
ColorStateList |
See also:
getItemPaddingBottom
public int getItemPaddingBottom ()
Get the distance from the bottom of an item's label to the bottom of the navigation bar item.
Returns | |
---|---|
int |
getItemPaddingTop
public int getItemPaddingTop ()
Get the distance from the top of an item's icon/active indicator to the top of the navigation bar item.
Returns | |
---|---|
int |
getItemRippleColor
public 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.
Returns | |
---|---|
ColorStateList |
See also:
getItemTextAppearanceActive
public 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
public 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
public ColorStateList getItemTextColor ()
Returns colors used for the different states (normal, selected, focused, etc.) of the menu item text.
Returns | |
---|---|
ColorStateList |
the ColorStateList of colors used for the different states of the menu items text. |
See also:
getLabelVisibilityMode
public int getLabelVisibilityMode ()
Returns the current label visibility mode used by this NavigationBarView
.
Returns | |
---|---|
int |
See also:
getMaxItemCount
public abstract int getMaxItemCount ()
Returns the maximum number of items that can be shown in NavigationBarView.
Returns | |
---|---|
int |
getMenu
public Menu getMenu ()
Returns the Menu
instance associated with this navigation bar.
Returns | |
---|---|
Menu |
getOrCreateBadge
public 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
public int getSelectedItemId ()
Returns the currently selected menu item ID, or zero if there is no menu.
Returns | |
---|---|
int |
See also:
inflateMenu
public 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
|
isItemActiveIndicatorEnabled
public boolean isItemActiveIndicatorEnabled ()
Get whether or not a selected item should show an active indicator.
Returns | |
---|---|
boolean |
true if an active indicator will be shown when an item is selected. |
removeBadge
public 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.
|
setActiveIndicatorLabelPadding
public void setActiveIndicatorLabelPadding (int activeIndicatorLabelPadding)
Set the distance between the active indicator container and the item's label.
Parameters | |
---|---|
activeIndicatorLabelPadding |
int |
setElevation
public void setElevation (float elevation)
Sets the base elevation of this view, in pixels.
Parameters | |
---|---|
elevation |
float |
setItemActiveIndicatorColor
public void setItemActiveIndicatorColor (ColorStateList csl)
Set the ColorStateList
of the active indicator drawable.
Parameters | |
---|---|
csl |
ColorStateList : The ColorStateList used as the color of the active indicator.
|
setItemActiveIndicatorEnabled
public void setItemActiveIndicatorEnabled (boolean enabled)
Set whether a selected item should show an active indicator.
Parameters | |
---|---|
enabled |
boolean : true if a selected item should show an active indicator.
|
setItemActiveIndicatorHeight
public void setItemActiveIndicatorHeight (int height)
Set the height of an item's active indicator.
Parameters | |
---|---|
height |
int : The height, in pixels, of the menu item's active indicator.
|
setItemActiveIndicatorMarginHorizontal
public void setItemActiveIndicatorMarginHorizontal (int horizontalMargin)
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.
Parameters | |
---|---|
horizontalMargin |
int : The horizontal margin, in pixels.
|
setItemActiveIndicatorShapeAppearance
public void setItemActiveIndicatorShapeAppearance (ShapeAppearanceModel shapeAppearance)
Set the ShapeAppearanceModel
of the active indicator drawable.
Parameters | |
---|---|
shapeAppearance |
ShapeAppearanceModel : The ShapeAppearanceModel of the active indicator drawable.
|
setItemActiveIndicatorWidth
public void setItemActiveIndicatorWidth (int width)
Set the width of an item's active indicator.
Parameters | |
---|---|
width |
int : The width, in pixels, of the menu item's active indicator.
|
setItemBackground
public 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)
.
Parameters | |
---|---|
background |
Drawable : The drawable for the background. |
setItemBackgroundResource
public 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)
.
Parameters | |
---|---|
resId |
int : The identifier of the resource. |
setItemIconSize
public 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.
Parameters | |
---|---|
iconSize |
int : the size in pixels to provide for the menu item icons |
setItemIconSizeRes
public 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.
Parameters | |
---|---|
iconSizeRes |
int : the resource ID for the size to provide for the menu item icons |
setItemIconTintList
public void setItemIconTintList (ColorStateList tint)
Set the tint which is applied to our menu items' icons.
Parameters | |
---|---|
tint |
ColorStateList : the tint to apply. |
setItemOnTouchListener
public void setItemOnTouchListener (int menuItemId, View.OnTouchListener onTouchListener)
Sets an View.OnTouchListener
for the item view associated with the
provided menuItemId
.
Parameters | |
---|---|
menuItemId |
int |
onTouchListener |
View.OnTouchListener |
setItemPaddingBottom
public void setItemPaddingBottom (int paddingBottom)
Set the distance from the bottom of an item's label to the bottom of the navigation bar item.
Parameters | |
---|---|
paddingBottom |
int |
setItemPaddingTop
public void setItemPaddingTop (int paddingTop)
Set the distance from the top of an items icon/active indicator to the top of the navigation bar item.
Parameters | |
---|---|
paddingTop |
int |
setItemRippleColor
public void setItemRippleColor (ColorStateList itemRippleColor)
Set the background of our menu items to be a ripple with the given colors.
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
public 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
|
setItemTextAppearanceActiveBoldEnabled
public void setItemTextAppearanceActiveBoldEnabled (boolean isBold)
Sets whether the active menu item labels are bold.
Parameters | |
---|---|
isBold |
boolean : whether the active menu item labels are bold
|
setItemTextAppearanceInactive
public 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
public void setItemTextColor (ColorStateList textColor)
Set the colors to use for the different states (normal, selected, focused, etc.) of the menu item text.
Parameters | |
---|---|
textColor |
ColorStateList |
See also:
setLabelVisibilityMode
public 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:
setOnItemReselectedListener
public void setOnItemReselectedListener (NavigationBarView.OnItemReselectedListener listener)
Set a listener that will be notified when the currently selected navigation item is reselected.
This does not require an NavigationBarView.OnItemSelectedListener
to be set.
Parameters | |
---|---|
listener |
NavigationBarView.OnItemReselectedListener : The listener to notify |
setOnItemSelectedListener
public void setOnItemSelectedListener (NavigationBarView.OnItemSelectedListener listener)
Set a listener that will be notified when a navigation item is selected. This listener will
also be notified when the currently selected item is reselected, unless an NavigationBarView.OnItemReselectedListener
has also been set.
Parameters | |
---|---|
listener |
NavigationBarView.OnItemSelectedListener : The listener to notify |
setSelectedItemId
public 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
protected void onAttachedToWindow ()
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-10-09 UTC.