NavigationView
public
class
NavigationView
extends FrameLayout
java.lang.Object | ||||
↳ | android.view.View | |||
↳ | android.view.ViewGroup | |||
↳ | android.widget.FrameLayout | |||
↳ | com.google.android.material.navigation.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 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 classes | |
---|---|
interface |
NavigationView.OnNavigationItemSelectedListener
Listener for handling events on navigation items. |
class |
NavigationView.SavedState
User interface state that is stored by NavigationView for implementing onSaveInstanceState(). |
XML attributes | |
---|---|
NavigationView_itemBackground |
|
NavigationView_itemHorizontalPadding |
|
NavigationView_itemIconPadding |
|
NavigationView_itemIconSize |
|
NavigationView_itemIconTint |
|
NavigationView_itemMaxLines |
|
NavigationView_itemTextAppearance |
|
NavigationView_itemTextAppearanceActiveBoldEnabled |
|
NavigationView_itemTextColor |
|
NavigationView_itemVerticalPadding |
Inherited constants |
---|
Inherited fields |
---|
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
|
cancelBackProgress()
|
void
|
draw(Canvas canvas)
|
MenuItem
|
getCheckedItem()
Returns the currently checked item in this navigation menu. |
int
|
getDividerInsetEnd()
Get the distance between the end of a divider and the end of the NavigationView. |
int
|
getDividerInsetStart()
Get the distance between the start edge of the NavigationView and the start of a menu divider. |
int
|
getHeaderCount()
Gets the number of headers in this NavigationView. |
View
|
getHeaderView(int index)
Gets the header view at the specified position. |
Drawable
|
getItemBackground()
Returns the background drawable for our menu items. |
int
|
getItemHorizontalPadding()
Returns the horizontal (left and right) padding in pixels applied to menu items. |
int
|
getItemIconPadding()
Returns the padding in pixels between the icon (if present) and the text of menu items. |
ColorStateList
|
getItemIconTintList()
Returns the tint which is applied to our menu items' icons. |
int
|
getItemMaxLines()
Gets the android:maxLines attribute of the text view in the menu item. |
ColorStateList
|
getItemTextColor()
Returns the tint which is applied to our menu items' icons. |
int
|
getItemVerticalPadding()
Returns the vertical (top and bottom) padding in pixels applied to menu items. |
Menu
|
getMenu()
Returns the |
int
|
getSubheaderInsetEnd()
Get the distance between the end of a menu subheader and the end of the NavigationView. |
int
|
getSubheaderInsetStart()
Get the distance between the start of the NavigationView and the start of a menu subheader. |
void
|
handleBackInvoked()
|
View
|
inflateHeaderView(int res)
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
|
isBottomInsetScrimEnabled()
Whether or not the NavigationView will draw a scrim behind the window's bottom inset. |
boolean
|
isTopInsetScrimEnabled()
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
|
setCheckedItem(int id)
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
|
setItemBackground(Drawable itemBackground)
Set the background of our menu items to a given resource. |
void
|
setItemBackgroundResource(int resId)
Set the background of our menu items to the given resource. |
void
|
setItemHorizontalPadding(int padding)
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
|
setItemTextAppearance(int resId)
Set the text appearance of the menu items to a given resource. |
void
|
setItemTextAppearanceActiveBoldEnabled(boolean isBold)
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
|
setItemVerticalPadding(int padding)
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
|
setNavigationItemSelectedListener(NavigationView.OnNavigationItemSelectedListener listener)
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
|
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)
|
void
|
updateBackProgress(BackEventCompat backEvent)
|
Protected methods | |
---|---|
void
|
dispatchDraw(Canvas canvas)
|
void
|
onAttachedToWindow()
|
void
|
onDetachedFromWindow()
|
void
|
onInsetsChanged(WindowInsetsCompat insets)
|
void
|
onMeasure(int widthSpec, int heightSpec)
|
void
|
onRestoreInstanceState(Parcelable savedState)
|
Parcelable
|
onSaveInstanceState()
|
void
|
onSizeChanged(int w, int h, int oldw, int oldh)
|
Inherited methods | |
---|---|
XML attributes
NavigationView_itemBackground
Related methods:
NavigationView_itemHorizontalPadding
Related methods:
NavigationView_itemIconPadding
Related methods:
NavigationView_itemIconSize
Related methods:
NavigationView_itemIconTint
Related methods:
NavigationView_itemMaxLines
Related methods:
NavigationView_itemTextAppearance
Related methods:
NavigationView_itemTextAppearanceActiveBoldEnabled
Related methods:
NavigationView_itemTextColor
Related methods:
NavigationView_itemVerticalPadding
Related methods:
Public constructors
NavigationView
public NavigationView (Context context)
Parameters | |
---|---|
context |
Context |
NavigationView
public NavigationView (Context context, AttributeSet attrs)
Parameters | |
---|---|
context |
Context |
attrs |
AttributeSet |
NavigationView
public NavigationView (Context context, AttributeSet attrs, int defStyleAttr)
Parameters | |
---|---|
context |
Context |
attrs |
AttributeSet |
defStyleAttr |
int |
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 ()
draw
public void draw (Canvas canvas)
Parameters | |
---|---|
canvas |
Canvas |
getCheckedItem
public MenuItem getCheckedItem ()
Returns the currently checked item in this navigation menu.
Returns | |
---|---|
MenuItem |
getDividerInsetEnd
public int getDividerInsetEnd ()
Get the distance between the end of a divider and the end of the NavigationView.
Returns | |
---|---|
int |
getDividerInsetStart
public int getDividerInsetStart ()
Get the distance between the start edge of the NavigationView and the start of a menu divider.
Returns | |
---|---|
int |
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 | |
---|---|
index |
int : 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.
Related XML Attributes:
Returns | |
---|---|
Drawable |
See also:
getItemHorizontalPadding
public int getItemHorizontalPadding ()
Returns the horizontal (left and right) padding in pixels applied to menu items.
Related XML Attributes:
Returns | |
---|---|
int |
See also:
getItemIconPadding
public int getItemIconPadding ()
Returns the padding in pixels between the icon (if present) and the text of menu items.
Related XML Attributes:
Returns | |
---|---|
int |
See also:
getItemIconTintList
public ColorStateList getItemIconTintList ()
Returns the tint which is applied to our menu items' icons.
Related XML Attributes:
Returns | |
---|---|
ColorStateList |
See also:
getItemMaxLines
public int getItemMaxLines ()
Gets the android:maxLines attribute of the text view in the menu item.
Related XML Attributes:
Returns | |
---|---|
int |
getItemTextColor
public ColorStateList getItemTextColor ()
Returns the tint which is applied to our menu items' icons.
Related XML Attributes:
Returns | |
---|---|
ColorStateList |
See also:
getItemVerticalPadding
public int getItemVerticalPadding ()
Returns the vertical (top and bottom) padding in pixels applied to menu items.
Related XML Attributes:
Returns | |
---|---|
int |
See also:
getMenu
public Menu getMenu ()
Returns the Menu
instance associated with this navigation view.
Returns | |
---|---|
Menu |
getSubheaderInsetEnd
public int getSubheaderInsetEnd ()
Get the distance between the end of a menu subheader and the end of the NavigationView.
Returns | |
---|---|
int |
getSubheaderInsetStart
public int getSubheaderInsetStart ()
Get the distance between the start of the NavigationView and the start of a menu subheader.
Returns | |
---|---|
int |
handleBackInvoked
public void handleBackInvoked ()
inflateHeaderView
public View inflateHeaderView (int res)
Inflates a View and add it as a header of the navigation menu.
Parameters | |
---|---|
res |
int : 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 | |
---|---|
resId |
int : 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.
Returns | |
---|---|
boolean |
isTopInsetScrimEnabled
public boolean isTopInsetScrimEnabled ()
Whether or not the NavigationView will draw a scrim behind the window's top inset.
Returns | |
---|---|
boolean |
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 | |
---|---|
enabled |
boolean : true when the NavigationView should draw a scrim.
|
setCheckedItem
public void setCheckedItem (MenuItem checkedItem)
Sets the currently checked item in this navigation menu.
Parameters | |
---|---|
checkedItem |
MenuItem : 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 | |
---|---|
id |
int : 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.
Parameters | |
---|---|
dividerInsetEnd |
int |
setDividerInsetStart
public void setDividerInsetStart (int dividerInsetStart)
Set the distance between the start edge of the NavigationView and the start of a menu divider.
Parameters | |
---|---|
dividerInsetStart |
int |
setDrawBottomInsetForeground
public void setDrawBottomInsetForeground (boolean drawBottomInsetForeground)
Parameters | |
---|---|
drawBottomInsetForeground |
boolean |
setDrawLeftInsetForeground
public void setDrawLeftInsetForeground (boolean drawLeftInsetForeground)
Parameters | |
---|---|
drawLeftInsetForeground |
boolean |
setDrawRightInsetForeground
public void setDrawRightInsetForeground (boolean drawRightInsetForeground)
Parameters | |
---|---|
drawRightInsetForeground |
boolean |
setDrawTopInsetForeground
public void setDrawTopInsetForeground (boolean drawTopInsetForeground)
Parameters | |
---|---|
drawTopInsetForeground |
boolean |
setElevation
public void setElevation (float elevation)
Parameters | |
---|---|
elevation |
float |
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.
Related XML Attributes:
Parameters | |
---|---|
itemBackground |
Drawable |
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.
Related XML Attributes:
Parameters | |
---|---|
resId |
int : The identifier of the resource. |
setItemHorizontalPadding
public void setItemHorizontalPadding (int padding)
Set the horizontal (left and right) padding in pixels of menu items.
Related XML Attributes:
Parameters | |
---|---|
padding |
int : The horizontal padding in pixels. |
setItemHorizontalPaddingResource
public void setItemHorizontalPaddingResource (int paddingResource)
Set the horizontal (left and right) padding of menu items.
Related XML Attributes:
Parameters | |
---|---|
paddingResource |
int : 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.
Related XML Attributes:
Parameters | |
---|---|
padding |
int : The padding in pixels. |
setItemIconPaddingResource
public void setItemIconPaddingResource (int paddingResource)
Set the padding between the icon (if present) and the text of menu items.
Related XML Attributes:
Parameters | |
---|---|
paddingResource |
int : 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.
Related XML Attributes:
Parameters | |
---|---|
iconSize |
int |
setItemIconTintList
public 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. |
setItemMaxLines
public void setItemMaxLines (int itemMaxLines)
Sets the android:maxLines attribute of the text view in the menu item.
Related XML Attributes:
Parameters | |
---|---|
itemMaxLines |
int |
setItemTextAppearance
public void setItemTextAppearance (int resId)
Set the text appearance of the menu items to a given resource.
Related XML Attributes:
Parameters | |
---|---|
resId |
int |
setItemTextAppearanceActiveBoldEnabled
public void setItemTextAppearanceActiveBoldEnabled (boolean isBold)
Sets whether the active menu item label is bold.
Related XML Attributes:
Parameters | |
---|---|
isBold |
boolean |
setItemTextColor
public void setItemTextColor (ColorStateList textColor)
Set the text color to be used on our menu items.
Related XML Attributes:
Parameters | |
---|---|
textColor |
ColorStateList |
See also:
setItemVerticalPadding
public void setItemVerticalPadding (int padding)
Set the vertical (top and bottom) padding in pixels of menu items.
Related XML Attributes:
Parameters | |
---|---|
padding |
int : The vertical padding in pixels. |
setItemVerticalPaddingResource
public void setItemVerticalPaddingResource (int paddingResource)
Set the vertical (top and bottom) padding of menu items.
Related XML Attributes:
Parameters | |
---|---|
paddingResource |
int : 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 | |
---|---|
listener |
NavigationView.OnNavigationItemSelectedListener : The listener to notify
|
setOverScrollMode
public void setOverScrollMode (int overScrollMode)
Parameters | |
---|---|
overScrollMode |
int |
setScrimInsetForeground
public void setScrimInsetForeground (Drawable drawable)
Sets the drawable used for the inset foreground.
Parameters | |
---|---|
drawable |
Drawable |
setSubheaderInsetEnd
public void setSubheaderInsetEnd (int subheaderInsetEnd)
Set the distance between the end of a menu subheader and the end of the NavigationView.
Parameters | |
---|---|
subheaderInsetEnd |
int |
setSubheaderInsetStart
public void setSubheaderInsetStart (int subheaderInsetStart)
Set the distance between the start of the NavigationView and the start of a menu subheader.
Parameters | |
---|---|
subheaderInsetStart |
int |
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 | |
---|---|
enabled |
boolean : true when the NavigationView should draw a scrim.
|
startBackProgress
public void startBackProgress (BackEventCompat backEvent)
Parameters | |
---|---|
backEvent |
BackEventCompat |
updateBackProgress
public void updateBackProgress (BackEventCompat backEvent)
Parameters | |
---|---|
backEvent |
BackEventCompat |
Protected methods
dispatchDraw
protected void dispatchDraw (Canvas canvas)
Parameters | |
---|---|
canvas |
Canvas |
onAttachedToWindow
protected void onAttachedToWindow ()
onDetachedFromWindow
protected void onDetachedFromWindow ()
onInsetsChanged
protected void onInsetsChanged (WindowInsetsCompat insets)
Parameters | |
---|---|
insets |
WindowInsetsCompat |
onMeasure
protected void onMeasure (int widthSpec, int heightSpec)
Parameters | |
---|---|
widthSpec |
int |
heightSpec |
int |
onRestoreInstanceState
protected void onRestoreInstanceState (Parcelable savedState)
Parameters | |
---|---|
savedState |
Parcelable |
onSaveInstanceState
protected Parcelable onSaveInstanceState ()
Returns | |
---|---|
Parcelable |
onSizeChanged
protected void onSizeChanged (int w, int h, int oldw, int oldh)
Parameters | |
---|---|
w |
int |
h |
int |
oldw |
int |
oldh |
int |
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-12-15 UTC.