NavigationView In Android With Examples

In this tutorial, we’ll discuss and implement a NavigationView in our android application. Here, we’ll learn to style it such that it opens from right to left too.


We have already implemented a Navigation Drawer in this tutorial and it was tiresome to code.

NavigationView is a better and easier to implement alternative to a Navigation Drawer. NavigationDrawer required us to implement the items using a ListView/RecyclerView by implementing a custom Adapter.

With the introduction of a NavigationView all we require is to inflate the items using menu resources that we’ll see soon. NavigationView is generally placed inside a DrawerLayout.

NavigationView Getting Started

Android Studio provides us a ready made Navigation Drawer Activity that implements a standard Navigation Menu. You can choose it from the following dialog.


Understanding the NavigationView

The NavigationView class extends FrameLayout. It’s defined in the xml under the tag as:

The NavigationView essentially consists of two major components:

  1. HeaderView : This View is typically displayed at the top of the Navigation Drawer. It essentially holds the profile picture, name email address and a background cover pic. This view is defined in a separate layout file that we’ll look at in a bit. To add the layout into our NavigationView, the app:headerLayout parameter is used
  2. Menu : This is displayed below the HeaderView and it contains all the navigation items in the form of a list. The layout file for this is defined in the menus folder. To add the layout into the NavigationView , the app:menus parameter is used

Other important XML attributes that are used to customise the NavigationView are:

  1. app:itemTextColor : This changes the text color
  2. app:itemIconTint : This changes the icon color
  3. app:itemBackground : This changes the item background color

Let’s look into the Project Structure of the inbuilt NavigationView template.

The activity_main.xml is the layout for the MainActivity.

Note: The above DrawerLayout is the layout that holds the navigation drawer content and our app’s content.

The app_bar_main.xml layout consists of a CoordinatorLayout that holds a ToolBar, a FloatingActionButton and a content_main.xml layout(which displays a basic ‘Hello World’ TextView). The layouts are listed below.


The content_main.xml layout is listed below:

The default headerLayout and the menu for the NavigationView are listed below:



The android:checkableBehavior xml attribute is defined for the entire group and it takes either of the three values listed below.

  1. single : Only one item from the group can be checked
  2. all : All items can be checked (checkboxes)
  3. none : No items are checkable

The android:checkable attribute is used for setting the checkable behaviour for individual items. It accepts boolean values.

Note: Nested menu items are possible inside the app:menus layout

The is given below

Important inferences drawn from the above piece of code are given below:

  1. The MainActivity implements NavigationView.OnNavigationItemSelectedListener and overrides the method onNavigationItemSelected. We handle the menu item clicks here and close the Drawer towards the left. Let’s display a Toast message for each of the items as show below.
  2. The ActionBarDrawerToggle is initialised as:

    The ActionBarDrawerToggle is used with a DrawerLayout to implement the recommended functionality of Navigation Drawers. It has the following usages:

    • It acts as a listener, for opening and closing of drawers.
    • It provides the hamburger icons in the ToolBar/ActionBar.
    • It’s allows for the animation between the hamburger icon and the arrow to exist.

    Note: is deprecated. Always use as a replacement.

  3. To add a listener on the DrawerLayout the following method is used.
    This listener is used to keep notified of drawer events.Note: drawer.setDrawerListener(toggle) is now deprecated.
  4. toggle.syncState() : will synchronise the icon’s state and display the hamburger icon or back arrow depending on whether the drawer is closed or open. Omitting this line of code won’t change the back arrow to the hamburger icon when the drawer is closed.
  5. drawer.closeDrawer(GravityCompat.START) : is used to close the drawer by setting the gravity to START(left by default)

Here’s how the default NavigationView looks like in an application:


Note that the last clicked item always stays highlighted in the first group. To remove the highlight as soon as the drawer is closed change the android:checkableBehavior to “none”.

The current NavigationView is drawn over the status bar. To put it beneath the status bar set the android:fitsSystemWindows as “false” for the NavigationView.


Now with the above attributes set we can further style the NavigationView by putting it under the ToolBar/ActionBar(Though this is not recommended in the Material Design Guidelines) by setting android:layout_marginTop=”?attr/actionBarSize” in the NavigationView and also setting android:fitsSystemWindows=”false” for CoordinatorLayout and DrawerLayout.

With the above customisation done, this is how the output looks :
android navigation view

You see the white status bar at the top? It’s due to the android:fitSystemWindows set to false for CoordinatorLayout and the DrawerLayout.
Styling the status bar in the styles.xml like
@color/colorPrimaryDark won’t change it. We need a better approach.

The only alternative is to get rid of the CoordinatorLayout (we aren’t using it’s animation either) and to put the DrawerLayout and ToolBar inside a LinearLayout.

Here are the update xml layouts:


android:fitSystemWindows=”true” is necessary in the Toolbar. Omitting it, you’ll end up with something like this!

android navigationview to avoid

Note: Removing the xml attribute android:theme="@style/AppTheme.AppBarOverlay" would change the ToolBar items color to black. Give it a go!


This is how the application looks now.

android navigation view below toolbar ideal

Oh wait! The status bar color is identical with the ToolBar. It was supposed to be a shade darker.
Just remove the following line from v-21/styles.xml

Let’s customise the NavigationView such that it opens from right to left!

Project Structure

We’ll be adding our own hamburger icon png file into the drawable folder as shown below.


Android NavigationView Example Code

The activity_main.xml layout is now defined as

We’ve placed the ToolBar with a FrameLayout inside a RelativeLayout. android:fitSystemWindows needs to be set true in all three of them.

The DrawerLayout contains tools:openDrawer="end" and android:layout_gravity="end" which changes the default side of the drawer to right.

Ideally a circular header image looks beautiful inside a NavigationView. We’ll compile the dependency de.hdodenhof.circleimageview.CircleImageView and use that in our nav_header_main.xml file as shown below.


The other xml layouts are identical to what were discussed above.

The is given below

Important inferences drawn from the above code are:

  1. toggle.setDrawerIndicatorEnabled(false); : This line is used to hide the default hamburger icon that was displayed on the left.
  2. All the GravityCompat constants are now changed to END instead of START.

The output of the application in action is given below.
android navigation view example right to left output

This brings an end to android NavigationView tutorial. You can download the final Android NavigationViewStyling Project from the link below.

By admin

Leave a Reply