Skip to content

Most visited

Recently visited

navigation
added in version 24.1.0
belongs to Maven artifact com.android.support:design:27.0.0

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

NavigationView_itemBackground  
NavigationView_itemIconTint  
NavigationView_itemTextAppearance  
NavigationView_itemTextColor  

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

Protected methods

void onAttachedToWindow()
void onDetachedFromWindow()
void onInsetsChanged(WindowInsetsCompat insets)
void onMeasure(int widthSpec, int heightSpec)
void onRestoreInstanceState(Parcelable savedState)
Parcelable onSaveInstanceState()

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

NavigationView_itemBackground

Related methods:

NavigationView_itemIconTint

Related methods:

NavigationView_itemTextAppearance

Related methods:

NavigationView_itemTextColor

Related methods:

Public constructors

NavigationView

added in version 24.1.0
NavigationView (Context context)

Parameters
context Context

NavigationView

added in version 24.1.0
NavigationView (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

NavigationView

added in version 24.1.0
NavigationView (Context context, 
                AttributeSet attrs, 
                int defStyleAttr)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

Public methods

addHeaderView

added in version 24.1.0
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)

Parameters
canvas Canvas

getHeaderCount

added in version 24.1.0
int getHeaderCount ()

Gets the number of headers in this NavigationView.

Returns
int A positive integer representing the number of headers.

getHeaderView

added in version 24.1.0
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

added in version 24.1.0
Drawable getItemBackground ()

Returns the background drawable for our menu items.

Related XML Attributes:

Returns
Drawable

See also:

getItemIconTintList

added in version 24.1.0
ColorStateList getItemIconTintList ()

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

Related XML Attributes:

Returns
ColorStateList

See also:

getItemTextColor

added in version 24.1.0
ColorStateList getItemTextColor ()

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

Related XML Attributes:

Returns
ColorStateList

See also:

getMenu

added in version 24.1.0
Menu getMenu ()

Returns the Menu instance associated with this navigation view.

Returns
Menu

inflateHeaderView

added in version 24.1.0
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

added in version 24.1.0
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

added in version 24.1.0
void removeHeaderView (View view)

Removes a previously-added header view.

Parameters
view View: The view to remove

setCheckedItem

added in version 24.1.0
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

added in version 24.1.0
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

added in version 24.1.0
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

added in version 24.1.0
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

added in version 24.1.0
void setItemTextAppearance (int resId)

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

Related XML Attributes:

Parameters
resId int

setItemTextColor

added in version 24.1.0
void setItemTextColor (ColorStateList textColor)

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

Related XML Attributes:

Parameters
textColor ColorStateList

See also:

setNavigationItemSelectedListener

added in version 24.1.0
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

Protected methods

onAttachedToWindow

void onAttachedToWindow ()

onDetachedFromWindow

void onDetachedFromWindow ()

onInsetsChanged

void onInsetsChanged (WindowInsetsCompat insets)

Parameters
insets WindowInsetsCompat

onMeasure

void onMeasure (int widthSpec, 
                int heightSpec)

Parameters
widthSpec int

heightSpec int

onRestoreInstanceState

void onRestoreInstanceState (Parcelable savedState)

Parameters
savedState Parcelable

onSaveInstanceState

Parcelable onSaveInstanceState ()

Returns
Parcelable

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)