- 1 Android Design Support Library: NavigationView
- 1.1 How does NavigationView work?
- 1.1.1 Two important parameters
- 1.1.2 Design time:
- 22.214.171.124 Step 1: Create a menu for navigation drawer items
- 126.96.36.199 Checkable menu items
- 188.8.131.52 Step 2: Create a header layout
- 184.108.40.206 Step 3: Get NavigationView and include menu and header layout
- 220.127.116.11 Step 4: Initialize Navigation view in activity class
- 18.104.22.168 Step 5: Assign navigation drawer item selected listener
- 1.2 Creating Navigation view By Programming
- 1.3 Other Points:
- 1.1 How does NavigationView work?
upport-library/">Android Design Support library. If you don’t know then let me tell you that this is the third post regarding Android material design and, today we are going to talk about NavigationView
In the previous two parts, we learned about Floating action Button(FAB) and android Snakebar, and today’s our topic is android NavigationView
When Google announced material design, we don’t have this much huge repository of components and tools, So developers like us were dependent on the third party tools and libraries, But thank god, with the release of Android design support library it’s very easy to implement Navigation drawer in any android application.
You surely know a headache of implementing navigation drawer before android design support library. But Now it’s like the knife in the cake let me explain how. I will tell you directly about NavigationView, which is the amazing component of the material design.
Before NavigationView, we were dependent on either linearLayout or ListView by defining a custom adapter for showing navigation drawer as per our need. But with NavigationView we can directly include it inside the same DrawerLayout.
Two important parameters
Here we are discussing two main essential parameters which are must require for set up NavigationView, Same as the navigation drawer in the inbox and Google Play Store applications.
app:menu – You can add a list of navigation drawer items, here we are going to create one normal menu file for the needed details.
app:headerlayout – which is header layout, like we Display profile picture and background image with the name and other items.
<android.support.design.widget.NavigationView android:id="@+id/navigation_view" android:layout_height="match_parent" android:layout_width="wrap_content" android:layout_gravity="start" android:fitsSystemWindows="true" app:headerLayout="@layout/drawer_header" app:menu="@menu/drawer_view"/>
Here are 2 ways we can implement a NavigationView, I am going to show you both the ways.
Design time, by preparing menu file with the items
Run time, by adding details into the Navigation view programmatically
Now, Let’s begin to code…
strong>Step 1: Create a menu for navigation drawer items
It can be created same as a menu xml file(drawer_view.xml), just make sure you create under the menu folder
<menu xmlns:android="http://schemas.android.com/apk/res/android"> <group android:checkableBehavior="single"> <item android:id="@+id/nav_home" android:icon="@drawable/ic_drawer_home" android:title="@string/nav_home" /> <item android:id="@+id/nav_about" android:icon="@drawable/ic_drawer_about" android:title="@string/nav_about" /> <item android:id="@+id/nav_settings" android:icon="@drawable/ic_drawer_settings" android:title="@string/nav_settings" /> <item android:id="@+id/navigation_subheader" android:title="@string/nav_sub_header"> <menu> <item android:id="@+id/navigation_sub_item_1" android:icon="@drawable/ic_drawer_about" android:title="@string/nav_sub_item_1" /> <item android:id="@+id/navigation_sub_item_2" android:icon="@drawable/ic_drawer_home" android:title="@string/nav_sub_item_2" /> </menu> </item> </group> </menu>
- single– Only one item can be checked (radio buttons)
- all– More than one items or All items can be checked (checkboxes)
- none– Not even single items are checkable