NavigationRailView MaterialDesign 1.4.0 Stable ๐
In this article, We will learn about NavigationRailView finally which hits the stable channel ๐ ๐
NavigationRailView : It is a side navigation component that displays 3 to 7 app destinations and, optionally, a Floating Action Button.
- Each destination is represented by an icon and a text label.
- The rail can function on its own at larger screen sizes, such as desktop and tablet.
- During the transition between screen sizes and devices, it can also complement other navigation components, such as bottom navigation.
Letโs see it in action ๐จโ๐ป๐
- Add dependency to Material Design library in your moduleโs
build.gradle
implementation("com.google.android.material:material:1.4.0")
I have created the sample project which uses the different layout based on the orientation of the device
- Portrait mode : Application will use
BottomNavigationView
- Landscape mode : Application use the
NavigationRailView
Layout Code for portrait mode :
Layout Code for landscape mode:
MainActivity code:
NavigationRailView properties deep dive:
- Set the menu :
nav_manu
refer to the menu layout file which is used to inflate the items inNavigationRailView
app:menu="@menu/nav_menu"
- Set the menu gravity :
- Set the label visibility mode for menu itemโs title :
- Set the header : It sets the header in
NavigationRailView
app:headerLayout="@layout/nav_header"
- Add/Remove Badge to item : Each item in
NavigationRailView
can have badge associates to it. You can add/remove badge to any item :
// it will create a new badge if there is no badge exist for provided menu itemgetOrCreateBadge(int menuItemId)
You can modify the various properties of badge :
- number
- badgeTextColor,
- maxCharacterCount,
- badgeGravity: (BadgeDrawable.TOP_START, BadgeDrawable.BOTTOM_END, etc.)
- setOnItemSelectedListener on
NavigationRailView
:
- setOnItemReselectedListener on
NavigationRailView
:
Demo
- Portrait mode :
- Landscape mode :
I have used
ViewBinding
for layout inflation, Check here to learn more aboutViewBinding
.