NavigationRailView

public class NavigationRailView extends NavigationBarView


Represents a standard navigation rail view for application. It is an implementation of Material Design navigation rail..

Navigation rails make it easy for users to explore and switch between top-level views in a single tap. They should be placed at the side edge of large screen devices such as tablets, when an application has three to seven top-level destinations.

The bar contents can be populated by specifying a menu resource file. Each menu item title, icon and enabled state will be used for displaying navigation rail bar items. Menu items can also be used for programmatically selecting which destination is currently active. It can be done using MenuItem#setChecked(true).

A header view (such as a , logo, etc.) can be added with the app:headerLayout attribute or by using addHeaderView.

layout resource file:
<com.google.android.material.navigationrail.NavigationRailView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schema.android.com/apk/res/res-auto"
    android:id="@+id/navigation"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    app:menu="@menu/my_navigation_items"
    app:headerLayout="@layout/my_navigation_rail_fab" />

res/menu/my_navigation_items.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/action_search"
         android:title="@string/menu_search"
         android:icon="@drawable/ic_search" />
    <item android:id="@+id/action_settings"
         android:title="@string/menu_settings"
         android:icon="@drawable/ic_add" />
    <item android:id="@+id/action_navigation"
         android:title="@string/menu_navigation"
         android:icon="@drawable/ic_action_navigation_menu" />
</menu>

res/layout/my_navigation_rail_fab.xml:
<com.google.android.material.floatingactionbutton.FloatingActionButton
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/my_navigation_rail_fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="@string/my_navigation_rail_fab_content_desc"
    app:srcCompat="@drawable/ic_add" />

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

Summary

Public fields

View
boolean

Public constructors

NavigationRailView(Context context)
NavigationRailView(Context context, AttributeSet attrs)
NavigationRailView(
    Context context,
    AttributeSet attrs,
    int defStyleAttr
)
NavigationRailView(
    Context context,
    AttributeSet attrs,
    int defStyleAttr,
    int defStyleRes
)

Public methods

void
addHeaderView(View headerView)

Adds the specified View if any, to appear at the top of the NavigationRailView.

void
addHeaderView(int layoutRes)

Adds the specified View layout resource, to appear at the top of the .

void

Collapse the navigation rail.

void

Expand the navigation rail.

int

Gets the minimum height of a navigation rail menu item when the navigation rail is collapsed.

int

Gets the minimum height of a navigation rail menu item when the navigation rail is expanded.

View

Returns reference to the header view if any, that is currently attached the .

int

Returns the navigation items' layout gravity.

int

Returns the current item icon gravity.

int

Get the current minimum height each item in the navigation rail's menu should be.

int

Get the current padding in between the navigation rail menu items.

int

Returns the maximum number of items that can be shown in NavigationBarView.

int

Gets the current gravity setting for how destinations in the menu view will be grouped.

boolean

Get whether or not to enable the dividers which go between each subgroup in the menu.

boolean

Returns whether or not the navigation rail is currently expanded.

boolean
onTouchEvent(MotionEvent event)
void

Removes the current header view if any, from the NavigationRailView.

void

Sets the minimum height of a navigation rail menu item when the navigation rail is collapsed.

void
setCollapsedItemSpacing(int itemSpacing)

Sets the padding in between the navigation rail menu items when the navigation rail is collapsed.

void

Sets the minimum height of a navigation rail menu item when the navigation rail is expanded.

void
setItemGravity(int itemGravity)

Sets the navigation items' layout gravity.

void
setItemIconGravity(int itemIconGravity)

Sets the navigation items' icon gravity.

void
setItemMinimumHeight(int minHeight)

Set the minimum height each item in the navigation rail's menu should use.

void
setItemSpacing(int itemSpacing)

Set the padding in between the navigation rail menu items.

void
setMenuGravity(int gravity)

Sets how destinations in the menu view will be grouped.

void
setSubmenuDividersEnabled(boolean submenuDividersEnabled)

Set whether or not to enable the dividers which go between each subgroup in the menu.

Protected methods

boolean

Returns whether or not submenus are supported.

void
onMeasure(int widthMeasureSpec, int heightMeasureSpec)

Inherited Constants

From com.google.android.material.navigation.NavigationBarView
static final int

The active indicator width fills up the width of its parent.

static final int

The active indicator width wraps the content.

static final int

Item is placed at the center of its container

static final int

Item is placed at the start center of its container

static final int

Item is placed at the top center of its container

static final int

Icon is placed at the top of the item

static final int

Icon is placed at the top of the item

static final int

Label behaves as "labeled" when there are 3 items or less, or "selected" when there are 4 items or more.

static final int

Label is shown on all navigation items.

static final int

Label is shown on the selected navigation item.

static final int

Label is not shown on any navigation items.

Inherited methods

From com.google.android.material.navigation.NavigationBarView
int

Get the distance between the active indicator container and the item's label.

BadgeDrawable
getBadge(int menuItemId)

Returns an instance of BadgeDrawable associated with menuItemId, null if none was initialized.

int

Returns the text appearance used for the active menu item label when they are in the horizontal item layout (when the start icon value is ITEM_ICON_GRAVITY_START).

int

Returns the text appearance used for inactive menu item labels when they are in the horizontal item layout (when the start icon value is ITEM_ICON_GRAVITY_START).

int

Get the horizontal distance between the icon and the item's label when the item is in the ITEM_ICON_GRAVITY_START configuration.

ColorStateList

Get the color of the active indicator drawable.

int

Get the height of an item's active indicator when it is expanded to wrap the item content, ie. when it is in the ITEM_ICON_GRAVITY_START configuration.

int

Get the margin that will be maintained at the start and end of the expanded active indicator away from the edges of its parent container.

int

Get the width of an item's active indicator when it is expanded to wrap the item content, ie. when it is in the ITEM_ICON_GRAVITY_START configuration.

int

Get the width of an item's active indicator.

int

Get the margin that will be maintained at the start and end of the active indicator away from the edges of its parent container.

ShapeAppearanceModel

Get the ShapeAppearanceModel of the active indicator drawable.

int

Get the width of an item's active indicator.

Drawable

Returns the background drawable of the menu items.

int

This method is deprecated.

Use getItemBackground instead.

int

Returns the size provided for the menu item icons in pixels.

ColorStateList

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

int

Get the distance from the bottom of an item's label to the bottom of the navigation bar item.

int

Get the distance from the top of an item's icon/active indicator to the top of the navigation bar item.

ColorStateList

Returns the color used to create a ripple as the background drawable of the menu items.

int

Returns the text appearance used for the active menu item label.

int

Returns the text appearance used for inactive menu item labels.

ColorStateList

Returns colors used for the different states (normal, selected, focused, etc.) of the menu item text.

int
getLabelMaxLines(int labelMaxLines)

Returns the max lines limit for the label text.

int

Returns the current label visibility mode used by this NavigationBarView.

Menu

Returns the Menu instance associated with this navigation bar.

ViewGroup

Returns the android.view.ViewGroup associated with the navigation bar menu.

BadgeDrawable
getOrCreateBadge(int menuItemId)

Creates an instance of BadgeDrawable associated with menuItemId if none exists.

boolean

Returns whether or not the label text should scale with the system font size.

int

Returns the currently selected menu item ID, or zero if there is no menu.

void
inflateMenu(int resId)

Inflate a menu resource into this navigation view.

boolean

Get whether or not a selected item should show an active indicator.

void
void
onRestoreInstanceState(Parcelable state)
Parcelable
void
removeBadge(int menuItemId)

Removes the BadgeDrawable associated with menuItemId.

void
setActiveIndicatorLabelPadding(int activeIndicatorLabelPadding)

Set the distance between the active indicator container and the item's label.

void
setElevation(float elevation)

Sets the base elevation of this view, in pixels.

void

Sets the text appearance to be used for the menu item labels when they are in the horizontal item layout (when the start icon value is ITEM_ICON_GRAVITY_START).

void

Sets the text appearance to be used for inactive menu item labels when they are in the horizontal item layout (when the start icon value is ITEM_ICON_GRAVITY_START).

void
setIconLabelHorizontalSpacing(int iconLabelSpacing)

Set the horizontal distance between the icon and the item's label when the item is in the ITEM_ICON_GRAVITY_START configuration.

void
setItemActiveIndicatorColor(ColorStateList csl)

Set the ColorStateList of the active indicator drawable.

void

Set whether a selected item should show an active indicator.

void

Set the height of an item's active indicator when it is expanded to wrap the item content, ie. when it is in the ITEM_ICON_GRAVITY_START configuration.

void

Set the horizontal margin that will be maintained at the start and end of the expanded active indicator, making sure the indicator maintains the given distance from the edge of its parent container.

void
setItemActiveIndicatorExpandedPadding(
    int paddingLeft,
    int paddingTop,
    int paddingRight,
    int paddingBottom
)

Set the padding of the expanded active indicator wrapping the content.

void

Set the width of an item's active indicator when it is expanded to wrap the item content, ie. when it is in the ITEM_ICON_GRAVITY_START configuration.

void

Set the height of an item's active indicator.

void

Set the horizontal margin that will be maintained at the start and end of the active indicator, making sure the indicator remains the given distance from the edge of its parent container.

void

Set the ShapeAppearanceModel of the active indicator drawable.

void

Set the width of an item's active indicator.

void
setItemBackground(Drawable background)

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

void

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

void
setItemIconSize(int iconSize)

Set the size to provide for the menu item icons.

void
setItemIconSizeRes(int iconSizeRes)

Set the size to provide for the menu item icons using a resource ID.

void
setItemIconTintList(ColorStateList tint)

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

void
setItemOnTouchListener(int menuItemId, OnTouchListener onTouchListener)

Sets an android.view.View.OnTouchListener for the item view associated with the provided menuItemId.

void
setItemPaddingBottom(int paddingBottom)

Set the distance from the bottom of an item's label to the bottom of the navigation bar item.

void
setItemPaddingTop(int paddingTop)

Set the distance from the top of an items icon/active indicator to the top of the navigation bar item.

void
setItemRippleColor(ColorStateList itemRippleColor)

Set the background of our menu items to be a ripple with the given colors.

void
setItemTextAppearanceActive(int textAppearanceRes)

Sets the text appearance to be used for the menu item labels.

void

Sets whether the active menu item labels are bold.

void
setItemTextAppearanceInactive(int textAppearanceRes)

Sets the text appearance to be used for inactive menu item labels.

void
setItemTextColor(ColorStateList textColor)

Set the colors to use for the different states (normal, selected, focused, etc.) of the menu item text.

void
setLabelFontScalingEnabled(boolean labelFontScalingEnabled)

Sets whether or not the label text should scale with the system font size.

void
setLabelMaxLines(int labelMaxLines)

Set the max lines limit for the label text.

void
setLabelVisibilityMode(int labelVisibilityMode)

Sets the navigation items' label visibility mode.

void

Set a listener that will be notified when the currently selected navigation item is reselected.

void

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

void
setSelectedItemId(int itemId)

Set the selected menu item ID.

Public fields

headerView

public View headerView
public boolean submenuDividersEnabled

Public constructors

public NavigationRailView(Context context)
public NavigationRailView(Context context, AttributeSet attrs)
public NavigationRailView(
    Context context,
    AttributeSet attrs,
    int defStyleAttr
)
public NavigationRailView(
    Context context,
    AttributeSet attrs,
    int defStyleAttr,
    int defStyleRes
)

Public methods

addHeaderView

public void addHeaderView(View headerView)

Adds the specified View if any, to appear at the top of the NavigationRailView. If the view already has a header view attached to it, it will be removed first.

Parameters
View headerView

reference to the View that should be attached.

addHeaderView

public void addHeaderView(int layoutRes)

Adds the specified View layout resource, to appear at the top of the . If the view already has a header view attached to it, it will be removed first.

Parameters
int layoutRes

the unique resource identifier to the layout that should be attached.

collapse

public void collapse()

Collapse the navigation rail.

expand

public void expand()

Expand the navigation rail.

getCollapsedItemMinimumHeight

public int getCollapsedItemMinimumHeight()

Gets the minimum height of a navigation rail menu item when the navigation rail is collapsed.

getExpandedItemMinimumHeight

public int getExpandedItemMinimumHeight()

Gets the minimum height of a navigation rail menu item when the navigation rail is expanded.

getHeaderView

public View getHeaderView()

Returns reference to the header view if any, that is currently attached the .

getItemGravity

public int getItemGravity()

Returns the navigation items' layout gravity.

See also
setItemGravity

getItemIconGravity

public int getItemIconGravity()

Returns the current item icon gravity.

getItemMinimumHeight

public int getItemMinimumHeight()

Get the current minimum height each item in the navigation rail's menu should be.

getItemSpacing

public int getItemSpacing()

Get the current padding in between the navigation rail menu items.

getMaxItemCount

public int getMaxItemCount()

Returns the maximum number of items that can be shown in NavigationBarView.

getMenuGravity

public int getMenuGravity()

Gets the current gravity setting for how destinations in the menu view will be grouped.

getSubmenuDividersEnabled

public boolean getSubmenuDividersEnabled()

Get whether or not to enable the dividers which go between each subgroup in the menu.

isExpanded

public boolean isExpanded()

Returns whether or not the navigation rail is currently expanded.

onTouchEvent

public boolean onTouchEvent(MotionEvent event)

removeHeaderView

public void removeHeaderView()

Removes the current header view if any, from the NavigationRailView.

setCollapsedItemMinimumHeight

public void setCollapsedItemMinimumHeight(int minHeight)

Sets the minimum height of a navigation rail menu item when the navigation rail is collapsed.

Parameters
int minHeight

the min height of the item when the nav rail is collapsed

setCollapsedItemSpacing

public void setCollapsedItemSpacing(int itemSpacing)

Sets the padding in between the navigation rail menu items when the navigation rail is collapsed.

Parameters
int itemSpacing

the desired item spacing in between the items when the nav rail is collapsed

setExpandedItemMinimumHeight

public void setExpandedItemMinimumHeight(int minHeight)

Sets the minimum height of a navigation rail menu item when the navigation rail is expanded.

Parameters
int minHeight

the min height of the item when the nav rail is collapsed

setItemGravity

public void setItemGravity(int itemGravity)

Sets the navigation items' layout gravity.

Parameters
int itemGravity

the layout android.view.Gravity of the item

See also
getItemGravity

setItemIconGravity

public void setItemIconGravity(int itemIconGravity)

Sets the navigation items' icon gravity.

Parameters
int itemIconGravity

the placement of the icon in the nav item one of ITEM_ICON_GRAVITY_TOP, or ITEM_ICON_GRAVITY_START

setItemMinimumHeight

public void setItemMinimumHeight(int minHeight)

Set the minimum height each item in the navigation rail's menu should use.

If this is unset (-1), each item will be at least as tall as the navigation rail is wide.

setItemSpacing

public void setItemSpacing(int itemSpacing)

Set the padding in between the navigation rail menu items.

setMenuGravity

public void setMenuGravity(int gravity)

Sets how destinations in the menu view will be grouped.

setSubmenuDividersEnabled

public void setSubmenuDividersEnabled(boolean submenuDividersEnabled)

Set whether or not to enable the dividers which go between each subgroup in the menu.

Protected methods

isSubMenuSupported

protected boolean isSubMenuSupported()

Returns whether or not submenus are supported.

onMeasure

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)