Most visited

Recently visited

NavigationView

public class NavigationView
extends FrameLayout

java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.FrameLayout
         ↳ android.support.design.widget.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.

 <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

android.support.design:itemBackground  
android.support.design:itemIconTint  
android.support.design:itemTextAppearance  
android.support.design:itemTextColor  

Inherited XML attributes

From class android.widget.FrameLayout
From class android.view.ViewGroup
From class android.view.View

Inherited constants

From class android.view.ViewGroup
From class android.view.View

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.

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.

ColorStateList getItemIconTintList()

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

ColorStateList getItemTextColor()

Returns the tint which is applied to our menu items' 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 our menu items to a given resource.

void setItemBackgroundResource(int resId)

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

void setItemIconTintList(ColorStateList tint)

Set the tint which is applied to our menu items' 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 to be used on our menu 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 onInsetsChanged(Rect insets)
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.

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

Related methods:

android.support.design:itemIconTint

Related methods:

android.support.design:itemTextAppearance

Related methods:

android.support.design:itemTextColor

Related methods:

Public constructors

NavigationView

NavigationView (Context context)

Parameters
context Context

NavigationView

NavigationView (Context context, 
                AttributeSet attrs)

Parameters
context Context
attrs AttributeSet

NavigationView

NavigationView (Context context, 
                AttributeSet attrs, 
                int defStyleAttr)

Parameters
context Context
attrs AttributeSet
defStyleAttr int

Public methods

addHeaderView

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.

draw

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 Canvas: The Canvas to which the View is rendered.

getHeaderCount

int getHeaderCount ()

Gets the number of headers in this NavigationView.

Returns
int A positive integer representing the number of headers.

getHeaderView

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

Drawable getItemBackground ()

Returns the background drawable for our menu items.

Related XML Attributes:

Returns
Drawable

See also:

getItemIconTintList

ColorStateList getItemIconTintList ()

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

Related XML Attributes:

Returns
ColorStateList

See also:

getItemTextColor

ColorStateList getItemTextColor ()

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

Related XML Attributes:

Returns
ColorStateList

See also:

getMenu

Menu getMenu ()

Returns the Menu instance associated with this navigation view.

Returns
Menu

inflateHeaderView

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

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

removeHeaderView

void removeHeaderView (View view)

Removes a previously-added header view.

Parameters
view View: The view to remove

setCheckedItem

void setCheckedItem (int id)

Sets the currently checked item in this navigation menu.

Parameters
id int: The item ID of the currently checked item.

setItemBackground

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

void setItemBackgroundResource (int resId)

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

Related XML Attributes:

Parameters
resId int: The identifier of the resource.

setItemIconTintList

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.

setItemTextAppearance

void setItemTextAppearance (int resId)

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

Related XML Attributes:

Parameters
resId int

setItemTextColor

void setItemTextColor (ColorStateList textColor)

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

Related XML Attributes:

Parameters
textColor ColorStateList

See also:

setNavigationItemSelectedListener

void setNavigationItemSelectedListener (NavigationView.OnNavigationItemSelectedListener listener)

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

Parameters
listener NavigationView.OnNavigationItemSelectedListener: The listener to notify

Protected methods

onAttachedToWindow

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

onDetachedFromWindow

void onDetachedFromWindow ()

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

onInsetsChanged

void onInsetsChanged (Rect insets)

Parameters
insets Rect

onMeasure

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 int: horizontal space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.
heightSpec int: vertical space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.

onRestoreInstanceState

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 Parcelable: The frozen state that had previously been returned by onSaveInstanceState().

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

Hooray!