NavigationView

public class NavigationView


Represents a standard navigation menu for application. The menu contents can be populated by a menu resource file.

NavigationView is typically placed inside a androidx.drawerlayout.widget.DrawerLayout.

<androidx.drawerlayout.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <!-- Your contents -->

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/navigation"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:menu="@menu/my_navigation_items" />
</androidx.drawerlayout.widget.DrawerLayout>

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

Summary

Nested types

Listener for handling events on navigation items.

User interface state that is stored by NavigationView for implementing onSaveInstanceState().

Public constructors

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

Public methods

void
addHeaderView(View view)

Adds a View as a header of the navigation menu.

void

Call this method from onBackCancelled or handleOnBackCancelled so that the back handler can cancel the back animation.

void
draw(Canvas canvas)
MenuItem

Returns the currently checked item in this navigation menu.

int

Get the distance between the end of a divider and the end of the NavigationView.

int

Get the distance between the start edge of the NavigationView and the start of a menu divider.

int

Gets the number of headers in this NavigationView.

View
getHeaderView(int index)

Gets the header view at the specified position.

Drawable

Returns the background drawable for our menu items.

int

Returns the horizontal (left and right) padding in pixels applied to menu items.

int

Returns the padding in pixels between the icon (if present) and the text of menu items.

ColorStateList

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

int

Gets the android:maxLines attribute of the text view in the menu item.

ColorStateList

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

int

Returns the vertical (top and bottom) padding in pixels applied to menu items.

Menu

Returns the Menu instance associated with this navigation view.

int

Get the distance between the end of a menu subheader and the end of the NavigationView.

int

Get the distance between the start of the NavigationView and the start of a menu subheader.

void

Call this method from onBackInvoked or handleOnBackPressed so that the back handler can complete the back animation, or handle back without progress in certain cases.

View

Inflates a View and add it as a header of the navigation menu.

void
inflateMenu(int resId)

Inflate a menu resource into this navigation view.

boolean

Whether or not the NavigationView will draw a scrim behind the window's bottom inset.

boolean

Whether or not the NavigationView will draw a scrim behind the window's end inset.

boolean

Whether or not the NavigationView will draw a scrim behind the window's start inset.

boolean

Whether or not the NavigationView will draw a scrim behind the window's top inset.

void
removeHeaderView(View view)

Removes a previously-added header view.

void
setBottomInsetScrimEnabled(boolean enabled)

Set whether or not the NavigationView should draw a scrim behind the window's bottom inset (typically the navigation bar)

void
setCheckedItem(MenuItem checkedItem)

Sets the currently checked item in this navigation menu.

void

Sets the currently checked item in this navigation menu.

void
setDividerInsetEnd(int dividerInsetEnd)

Set the distance between the end of a divider and the end of the NavigationView.

void
setDividerInsetStart(int dividerInsetStart)

Set the distance between the start edge of the NavigationView and the start of a menu divider.

void
setDrawBottomInsetForeground(boolean drawBottomInsetForeground)
void
setDrawLeftInsetForeground(boolean drawLeftInsetForeground)
void
setDrawRightInsetForeground(boolean drawRightInsetForeground)
void
setDrawTopInsetForeground(boolean drawTopInsetForeground)
void
setElevation(float elevation)
void
setEndInsetScrimEnabled(boolean enabled)

Set whether or not the NavigationView should draw a scrim behind the window's end inset.

void
setItemBackground(Drawable itemBackground)

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

void

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

void

Set the horizontal (left and right) padding in pixels of menu items.

void
setItemHorizontalPaddingResource(int paddingResource)

Set the horizontal (left and right) padding of menu items.

void
setItemIconPadding(int padding)

Set the padding in pixels between the icon (if present) and the text of menu items.

void
setItemIconPaddingResource(int paddingResource)

Set the padding between the icon (if present) and the text of menu items.

void
setItemIconSize(int iconSize)

Sets the size to be used for the menu item icons in pixels.

void
setItemIconTintList(ColorStateList tint)

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

void
setItemMaxLines(int itemMaxLines)

Sets the android:maxLines attribute of the text view in the menu item.

void

Set the text appearance of the menu items to a given resource.

void

Sets whether the active menu item label is bold.

void
setItemTextColor(ColorStateList textColor)

Set the text color to be used on our menu items.

void

Set the vertical (top and bottom) padding in pixels of menu items.

void
setItemVerticalPaddingResource(int paddingResource)

Set the vertical (top and bottom) padding of menu items.

void

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

void
setOverScrollMode(int overScrollMode)
void
setScrimInsetForeground(Drawable drawable)

Sets the drawable used for the inset foreground.

void
setStartInsetScrimEnabled(boolean enabled)

Set whether or not the NavigationView should draw a scrim behind the window's start inset.

void
setSubheaderInsetEnd(int subheaderInsetEnd)

Set the distance between the end of a menu subheader and the end of the NavigationView.

void
setSubheaderInsetStart(int subheaderInsetStart)

Set the distance between the start of the NavigationView and the start of a menu subheader.

void
setTopInsetScrimEnabled(boolean enabled)

Set whether or not the NavigationView should draw a scrim behind the window's top inset (typically the status bar).

void
startBackProgress(BackEventCompat backEvent)

Call this method from onBackStarted or handleOnBackStarted so that the back handler can initialize and start animating.

void
updateBackProgress(BackEventCompat backEvent)

Call this method from onBackProgressed or handleOnBackProgressed so that the back handler can continue animating with a new progress value.

Protected methods

void
dispatchDraw(Canvas canvas)
void
void
void
onMeasure(int widthSpec, int heightSpec)
void
onRestoreInstanceState(Parcelable savedState)
Parcelable
void
onSizeChanged(int w, int h, int oldw, int oldh)

Public constructors

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

Public methods

addHeaderView

public void addHeaderView(View view)

Adds a View as a header of the navigation menu.

Parameters
View view

The view to be added as a header of the navigation menu.

cancelBackProgress

public void cancelBackProgress()

Call this method from onBackCancelled or handleOnBackCancelled so that the back handler can cancel the back animation.

draw

public void draw(Canvas canvas)

getCheckedItem

public MenuItem getCheckedItem()

Returns the currently checked item in this navigation menu.

getDividerInsetEnd

public int getDividerInsetEnd()

Get the distance between the end of a divider and the end of the NavigationView.

getDividerInsetStart

public int getDividerInsetStart()

Get the distance between the start edge of the NavigationView and the start of a menu divider.

getHeaderCount

public int getHeaderCount()

Gets the number of headers in this NavigationView.

Returns
int

A positive integer representing the number of headers.

getHeaderView

public View getHeaderView(int index)

Gets the header view at the specified position.

Parameters
int index

The position at which to get the view from.

Returns
View

The header view the specified position or null if the position does not exist in this NavigationView.

getItemBackground

public Drawable getItemBackground()

Returns the background drawable for our menu items.

ref R.styleable#NavigationView_itemBackground

getItemHorizontalPadding

public int getItemHorizontalPadding()

Returns the horizontal (left and right) padding in pixels applied to menu items.

ref R.styleable#NavigationView_itemHorizontalPadding

getItemIconPadding

public int getItemIconPadding()

Returns the padding in pixels between the icon (if present) and the text of menu items.

ref R.styleable#NavigationView_itemIconPadding

getItemIconTintList

public ColorStateList getItemIconTintList()

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

ref R.styleable#NavigationView_itemIconTint

getItemMaxLines

public int getItemMaxLines()

Gets the android:maxLines attribute of the text view in the menu item.

ref R.styleable#NavigationView_itemMaxLines

getItemTextColor

public ColorStateList getItemTextColor()

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

ref R.styleable#NavigationView_itemTextColor

See also
setItemTextColor

getItemVerticalPadding

public int getItemVerticalPadding()

Returns the vertical (top and bottom) padding in pixels applied to menu items.

ref R.styleable#NavigationView_itemVerticalPadding

getMenu

public Menu getMenu()

Returns the Menu instance associated with this navigation view.

getSubheaderInsetEnd

public int getSubheaderInsetEnd()

Get the distance between the end of a menu subheader and the end of the NavigationView.

getSubheaderInsetStart

public int getSubheaderInsetStart()

Get the distance between the start of the NavigationView and the start of a menu subheader.

handleBackInvoked

public void handleBackInvoked()

Call this method from onBackInvoked or handleOnBackPressed so that the back handler can complete the back animation, or handle back without progress in certain cases.

inflateHeaderView

public View inflateHeaderView(int res)

Inflates a View and add it as a header of the navigation menu.

Parameters
int res

The layout resource ID.

Returns
View

a newly inflated View.

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

ID of a menu resource to inflate

isBottomInsetScrimEnabled

public boolean isBottomInsetScrimEnabled()

Whether or not the NavigationView will draw a scrim behind the window's bottom inset.

isEndInsetScrimEnabled

public boolean isEndInsetScrimEnabled()

Whether or not the NavigationView will draw a scrim behind the window's end inset.

isStartInsetScrimEnabled

public boolean isStartInsetScrimEnabled()

Whether or not the NavigationView will draw a scrim behind the window's start inset.

isTopInsetScrimEnabled

public boolean isTopInsetScrimEnabled()

Whether or not the NavigationView will draw a scrim behind the window's top inset.

removeHeaderView

public void removeHeaderView(View view)

Removes a previously-added header view.

Parameters
View view

The view to remove

setBottomInsetScrimEnabled

public void setBottomInsetScrimEnabled(boolean enabled)

Set whether or not the NavigationView should draw a scrim behind the window's bottom inset (typically the navigation bar)

Parameters
boolean enabled

true when the NavigationView should draw a scrim.

setCheckedItem

public void setCheckedItem(MenuItem checkedItem)

Sets the currently checked item in this navigation menu.

Parameters
MenuItem checkedItem

The checked item from the menu available from getMenu.

setCheckedItem

public void setCheckedItem(int id)

Sets the currently checked item in this navigation menu.

Parameters
int id

The item ID of the currently checked item.

setDividerInsetEnd

public void setDividerInsetEnd(int dividerInsetEnd)

Set the distance between the end of a divider and the end of the NavigationView.

setDividerInsetStart

public void setDividerInsetStart(int dividerInsetStart)

Set the distance between the start edge of the NavigationView and the start of a menu divider.

setDrawBottomInsetForeground

public void setDrawBottomInsetForeground(boolean drawBottomInsetForeground)

setDrawLeftInsetForeground

public void setDrawLeftInsetForeground(boolean drawLeftInsetForeground)

setDrawRightInsetForeground

public void setDrawRightInsetForeground(boolean drawRightInsetForeground)

setDrawTopInsetForeground

public void setDrawTopInsetForeground(boolean drawTopInsetForeground)

setElevation

public void setElevation(float elevation)

setEndInsetScrimEnabled

public void setEndInsetScrimEnabled(boolean enabled)

Set whether or not the NavigationView should draw a scrim behind the window's end inset.

Parameters
boolean enabled

true when the NavigationView should draw a scrim.

setItemBackground

public void setItemBackground(Drawable itemBackground)

Set the background of our menu items to a given resource. The resource should refer to a Drawable object or null to use the default background set on this navigation menu.

ref R.styleable#NavigationView_itemBackground

setItemBackgroundResource

public void setItemBackgroundResource(int resId)

Set the background of our menu items to the given resource. This overrides the default background set to items and it's styling.

ref R.styleable#NavigationView_itemBackground

Parameters
int resId

The identifier of the resource.

setItemHorizontalPadding

public void setItemHorizontalPadding(int padding)

Set the horizontal (left and right) padding in pixels of menu items.

ref R.styleable#NavigationView_itemHorizontalPadding

Parameters
int padding

The horizontal padding in pixels.

setItemHorizontalPaddingResource

public void setItemHorizontalPaddingResource(int paddingResource)

Set the horizontal (left and right) padding of menu items.

ref R.styleable#NavigationView_itemHorizontalPadding

Parameters
int paddingResource

Dimension resource to use for the horizontal padding.

setItemIconPadding

public void setItemIconPadding(int padding)

Set the padding in pixels between the icon (if present) and the text of menu items.

ref R.styleable#NavigationView_itemIconPadding

Parameters
int padding

The padding in pixels.

setItemIconPaddingResource

public void setItemIconPaddingResource(int paddingResource)

Set the padding between the icon (if present) and the text of menu items.

ref R.styleable#NavigationView_itemIconPadding

Parameters
int paddingResource

Dimension resource to use for the icon padding.

setItemIconSize

public void setItemIconSize(int iconSize)

Sets the size to be used for the menu item icons in pixels. If no icons are set, calling this method will do nothing.

ref R.styleable#NavigationView_itemIconSize

setItemIconTintList

public void setItemIconTintList(ColorStateList tint)

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

ref R.styleable#NavigationView_itemIconTint

Parameters
ColorStateList tint

the tint to apply.

setItemMaxLines

public void setItemMaxLines(int itemMaxLines)

Sets the android:maxLines attribute of the text view in the menu item.

ref R.styleable#NavigationView_itemMaxLines

setItemTextAppearance

public void setItemTextAppearance(int resId)

Set the text appearance of the menu items to a given resource.

ref R.styleable#NavigationView_itemTextAppearance

setItemTextAppearanceActiveBoldEnabled

public void setItemTextAppearanceActiveBoldEnabled(boolean isBold)

Sets whether the active menu item label is bold.

ref R.styleable#NavigationView_itemTextAppearanceActiveBoldEnabled

setItemTextColor

public void setItemTextColor(ColorStateList textColor)

Set the text color to be used on our menu items.

ref R.styleable#NavigationView_itemTextColor

See also
getItemTextColor

setItemVerticalPadding

public void setItemVerticalPadding(int padding)

Set the vertical (top and bottom) padding in pixels of menu items.

ref R.styleable#NavigationView_itemVerticalPadding

Parameters
int padding

The vertical padding in pixels.

setItemVerticalPaddingResource

public void setItemVerticalPaddingResource(int paddingResource)

Set the vertical (top and bottom) padding of menu items.

ref R.styleable#NavigationView_itemVerticalPadding

Parameters
int paddingResource

Dimension resource to use for the vertical padding.

setNavigationItemSelectedListener

public void setNavigationItemSelectedListener(
    NavigationView.OnNavigationItemSelectedListener listener
)

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

Parameters
NavigationView.OnNavigationItemSelectedListener listener

The listener to notify

setOverScrollMode

public void setOverScrollMode(int overScrollMode)

setScrimInsetForeground

public void setScrimInsetForeground(Drawable drawable)

Sets the drawable used for the inset foreground.

setStartInsetScrimEnabled

public void setStartInsetScrimEnabled(boolean enabled)

Set whether or not the NavigationView should draw a scrim behind the window's start inset.

Parameters
boolean enabled

true when the NavigationView should draw a scrim.

setSubheaderInsetEnd

public void setSubheaderInsetEnd(int subheaderInsetEnd)

Set the distance between the end of a menu subheader and the end of the NavigationView.

setSubheaderInsetStart

public void setSubheaderInsetStart(int subheaderInsetStart)

Set the distance between the start of the NavigationView and the start of a menu subheader.

setTopInsetScrimEnabled

public void setTopInsetScrimEnabled(boolean enabled)

Set whether or not the NavigationView should draw a scrim behind the window's top inset (typically the status bar).

Parameters
boolean enabled

true when the NavigationView should draw a scrim.

startBackProgress

public void startBackProgress(BackEventCompat backEvent)

Call this method from onBackStarted or handleOnBackStarted so that the back handler can initialize and start animating.

Note that this must be called prior to calling updateBackProgress.

updateBackProgress

public void updateBackProgress(BackEventCompat backEvent)

Call this method from onBackProgressed or handleOnBackProgressed so that the back handler can continue animating with a new progress value.

Protected methods

dispatchDraw

protected void dispatchDraw(Canvas canvas)

onAttachedToWindow

protected void onAttachedToWindow()

onDetachedFromWindow

protected void onDetachedFromWindow()

onMeasure

protected void onMeasure(int widthSpec, int heightSpec)

onRestoreInstanceState

protected void onRestoreInstanceState(Parcelable savedState)

onSaveInstanceState

protected Parcelable onSaveInstanceState()

onSizeChanged

protected void onSizeChanged(int w, int h, int oldw, int oldh)