NavigationRailView

public class NavigationRailView
extends NavigationBarView

java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.FrameLayout
         ↳ com.google.android.material.navigation.NavigationBarView
           ↳ com.google.android.material.navigationrail.NavigationRailView


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 FloatingActionButton, logo, etc.) can be added with the app:headerLayout attribute or by using addHeaderView(View).

 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" />
 

Summary

Inherited XML attributes

Inherited constants

Inherited fields

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(int layoutRes)

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

void addHeaderView(View headerView)

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

View getHeaderView()

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

int getMaxItemCount()

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

int getMenuGravity()

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

void removeHeaderView()

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

void setMenuGravity(int gravity)

Sets how destinations in the menu view will be grouped.

Protected methods

void onLayout(boolean changed, int left, int top, int right, int bottom)
void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

Inherited methods

Public constructors

public NavigationRailView (Context context)

Parameters
context Context

public NavigationRailView (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

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

Parameters
context Context

attrs AttributeSet

defStyleAttr int

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

Parameters
context Context

attrs AttributeSet

defStyleAttr int

defStyleRes int

Public methods

addHeaderView

public void addHeaderView (int layoutRes)

Adds the specified View layout resource, 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
layoutRes int: the unique resource identifier to the layout that should be attached.

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
headerView View: reference to the View that should be attached.

getHeaderView

public View getHeaderView ()

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

Returns
View

getMaxItemCount

public int getMaxItemCount ()

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

Returns
int

getMenuGravity

public int getMenuGravity ()

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

Returns
int

removeHeaderView

public void removeHeaderView ()

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

setMenuGravity

public void setMenuGravity (int gravity)

Sets how destinations in the menu view will be grouped.

Parameters
gravity int

Protected methods

onLayout

protected void onLayout (boolean changed, 
                int left, 
                int top, 
                int right, 
                int bottom)

Parameters
changed boolean

left int

top int

right int

bottom int

onMeasure

protected void onMeasure (int widthMeasureSpec, 
                int heightMeasureSpec)

Parameters
widthMeasureSpec int

heightMeasureSpec int