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

BadgeDrawable getBadge(int menuItemId)

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

Drawable getItemBackground()

Returns the background drawable of the menu items.

int getItemBackgroundResource()

This method is deprecated. Use getItemBackground() instead.

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

abstract int getMaxItemCount()

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

Menu getMenu()

Returns the Menu instance associated with this navigation bar.

BadgeDrawable getOrCreateBadge(int menuItemId)

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

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.

void removeBadge(int menuItemId)

Removes the BadgeDrawable associated with menuItemId.

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 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 View.OnTouchListener for the item view associated with the provided menuItemId.

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

NavigationBarPresenter getPresenter()
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

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

Parameters
context Context

attrs AttributeSet

defStyleAttr int

defStyleRes int

Public methods

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.

getItemBackground

public Drawable getItemBackground ()

Returns the background drawable of the menu items.

Returns
Drawable

getItemBackgroundResource

public int getItemBackgroundResource ()

This method is deprecated.
Use getItemBackground() instead.

Returns the background resource of the menu items.

Returns
int

getItemIconSize

public int getItemIconSize ()

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

Returns
int

getItemIconTintList

public ColorStateList getItemIconTintList ()

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

Returns
ColorStateList

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

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.

getLabelVisibilityMode

public int getLabelVisibilityMode ()

Returns the current label visibility mode used by this NavigationBarView.

Returns
int

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

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

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.

setElevation

public void setElevation (float elevation)

Sets the base elevation of this view, in pixels.

Parameters
elevation float

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

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

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

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.

Protected methods

getPresenter

protected NavigationBarPresenter getPresenter ()

Returns
NavigationBarPresenter

onAttachedToWindow

protected void onAttachedToWindow ()

onRestoreInstanceState

protected void onRestoreInstanceState (Parcelable state)

Parameters
state Parcelable

onSaveInstanceState

protected Parcelable onSaveInstanceState ()

Returns
Parcelable