Android APIs
public class

NavigationView

extends FrameLayout
java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.FrameLayout
         ↳ android.support.design.widget.NavigationView

Class Overview

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.

 <android.support.v4.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 -->

     <android.support.design.widget.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" />
 </android.support.v4.widget.DrawerLayout>
 

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
Attribute Name Related Method Description
android.support.design:itemBackground setItemBackgroundResource(int)  
android.support.design:itemIconTint setItemIconTintList(ColorStateList)  
android.support.design:itemTextAppearance setItemTextAppearance(int)  
android.support.design:itemTextColor setItemTextColor(ColorStateList)  
[Expand]
Inherited XML Attributes
From class android.widget.FrameLayout
From class android.view.ViewGroup
From class android.view.View
[Expand]
Inherited Constants
From class android.view.ViewGroup
From class android.view.View
[Expand]
Inherited Fields
From class android.view.View
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 draw(Canvas canvas)
Manually render this view (and all of its children) to the given Canvas.
Drawable getItemBackground()
Returns the background drawable for the menu items.
ColorStateList getItemIconTintList()
Returns the tint which is applied to our item's icons.
ColorStateList getItemTextColor()
Returns the tint which is applied to our item's icons.
Menu getMenu()
Returns the Menu instance associated with this navigation view.
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.
void removeHeaderView(View view)
Removes a previously-added header view.
void setCheckedItem(int id)
Sets the currently checked item in this navigation menu.
void setItemBackground(Drawable itemBackground)
Set the background of the menu items to a given resource.
void setItemBackgroundResource(int resId)
Set the background of the menu items to the given resource.
void setItemIconTintList(ColorStateList tint)
Set the tint which is applied to our item's icons.
void setItemTextAppearance(int resId)
Set the text appearance of the menu items to a given resource.
void setItemTextColor(ColorStateList textColor)
Set the text color which is text to our items.
void setNavigationItemSelectedListener(NavigationView.OnNavigationItemSelectedListener listener)
Set a listener that will be notified when a menu item is clicked.
Protected Methods
void onAttachedToWindow()
This is called when the view is attached to a window.
void onDetachedFromWindow()
This is called when the view is detached from a window.
void onMeasure(int widthSpec, int heightSpec)

Measure the view and its content to determine the measured width and the measured height.

void onRestoreInstanceState(Parcelable savedState)
Hook allowing a view to re-apply a representation of its internal state that had previously been generated by onSaveInstanceState().
Parcelable onSaveInstanceState()
Hook allowing a view to generate a representation of its internal state that can later be used to create a new instance with that same state.
[Expand]
Inherited Methods
From class android.widget.FrameLayout
From class android.view.ViewGroup
From class android.view.View
From class java.lang.Object
From interface android.view.ViewParent
From interface android.view.ViewManager
From interface android.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.accessibility.AccessibilityEventSource

XML Attributes

android.support.design:itemBackground

android.support.design:itemIconTint

android.support.design:itemTextAppearance

Related Methods

android.support.design:itemTextColor

Public Constructors

public NavigationView (Context context)

public NavigationView (Context context, AttributeSet attrs)

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

Public Methods

public void addHeaderView (View view)

Adds a View as a header of the navigation menu.

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

public void draw (Canvas canvas)

Manually render this view (and all of its children) to the given Canvas. The view must have already done a full layout before this function is called. When implementing a view, implement onDraw(android.graphics.Canvas) instead of overriding this method. If you do need to override this method, call the superclass version.

Parameters
canvas The Canvas to which the View is rendered.

public Drawable getItemBackground ()

Returns the background drawable for the menu items.

Related XML Attributes

public ColorStateList getItemIconTintList ()

Returns the tint which is applied to our item's icons.

Related XML Attributes

public ColorStateList getItemTextColor ()

Returns the tint which is applied to our item's icons.

Related XML Attributes

public Menu getMenu ()

Returns the Menu instance associated with this navigation view.

public View inflateHeaderView (int res)

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

Parameters
res The layout resource ID.
Returns
  • a newly inflated View.

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 ID of a menu resource to inflate

public void removeHeaderView (View view)

Removes a previously-added header view.

Parameters
view The view to remove

public void setCheckedItem (int id)

Sets the currently checked item in this navigation menu.

Parameters
id The item ID of the currently checked item.

public void setItemBackground (Drawable itemBackground)

Set the background of the menu items to a given resource. The resource should refer to a Drawable object or 0 to use the background background.

Related XML Attributes

public void setItemBackgroundResource (int resId)

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

Related XML Attributes
Parameters
resId The identifier of the resource.

public void setItemIconTintList (ColorStateList tint)

Set the tint which is applied to our item's icons.

Related XML Attributes
Parameters
tint the tint to apply.

public void setItemTextAppearance (int resId)

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

public void setItemTextColor (ColorStateList textColor)

Set the text color which is text to our items.

Related XML Attributes

public void setNavigationItemSelectedListener (NavigationView.OnNavigationItemSelectedListener listener)

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

Parameters
listener The listener to notify

Protected Methods

protected void onAttachedToWindow ()

This is called when the view is attached to a window. At this point it has a Surface and will start drawing. Note that this function is guaranteed to be called before onDraw(android.graphics.Canvas), however it may be called any time before the first onDraw -- including before or after onMeasure(int, int).

protected void onDetachedFromWindow ()

This is called when the view is detached from a window. At this point it no longer has a surface for drawing.

protected void onMeasure (int widthSpec, int heightSpec)

Measure the view and its content to determine the measured width and the measured height. This method is invoked by measure(int, int) and should be overridden by subclasses to provide accurate and efficient measurement of their contents.

CONTRACT: When overriding this method, you must call setMeasuredDimension(int, int) to store the measured width and height of this view. Failure to do so will trigger an IllegalStateException, thrown by measure(int, int). Calling the superclass' onMeasure(int, int) is a valid use.

The base class implementation of measure defaults to the background size, unless a larger size is allowed by the MeasureSpec. Subclasses should override onMeasure(int, int) to provide better measurements of their content.

If this method is overridden, it is the subclass's responsibility to make sure the measured height and width are at least the view's minimum height and width (getSuggestedMinimumHeight() and getSuggestedMinimumWidth()).

Parameters
widthSpec horizontal space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.
heightSpec vertical space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.

protected void onRestoreInstanceState (Parcelable savedState)

Hook allowing a view to re-apply a representation of its internal state that had previously been generated by onSaveInstanceState(). This function will never be called with a null state.

Parameters
savedState The frozen state that had previously been returned by onSaveInstanceState().

protected Parcelable onSaveInstanceState ()

Hook allowing a view to generate a representation of its internal state that can later be used to create a new instance with that same state. This state should only contain information that is not persistent or can not be reconstructed later. For example, you will never store your current position on screen because that will be computed again when a new instance of the view is placed in its view hierarchy.

Some examples of things you may store here: the current cursor position in a text view (but usually not the text itself since that is stored in a content provider or other persistent storage), the currently selected item in a list view.

Returns
  • Returns a Parcelable object containing the view's current dynamic state, or null if there is nothing interesting to save. The default implementation returns null.