Introduction
In this article, we will learn about NavigationView in Android with Java Programming Language.
NavigationView in Android
NavigationView is a simple way to access a Menu tool navigation chart. This is the most widely used method to incorporate content navigation drawers in combination with the DrawerLayout. Navigation drawers are modal elevated dialogues and are used to display in-app navigation links coming from the left/start side. NavigationView is a scrollable view that allows a menu feature as a vertical column.
Navigation Drawer in Android
The most common usage for NavigationView is to add navigation drawers. This section will teach you how to use NavigationView for that purpose.
How does NavigationView work in Android?
NavigationView in Android is placed in the Navigation Drawer. When we work with Navigation View we see three small lines on the left side of the toolbar on the home screen of the Android Application, when we click on it a navigation drawer opens from the left side of the screen as shown in the below image.
![navigation view]()
As we can see in the image, there are two screen images. On the first screen, there is an Activity that shows the message "Message is Selected" and the toolbar" NavigationViewExample" on the left side of the toolbar with three lines. On the second screen, we click these three lines, and our navigation view opens.
Now, we will see the example of Navigation View in Android Studio step by step.
Step 1. First, we will create an Android Studio project named Example. Android Studio will create two files in the project MainActivity.java and activity_main.xml.
Step 2. Before starting with NavigationView, we have to add the dependency that is used for NavigationView in our build.gradle code file of the application. Here is our build.gradle file.
We add the material dependency in the build.gradle file of the Android application, which is used for material design, and NavigationView is also a part of it. After adding the RecyclerView dependency to our project, sync the project.
Step 3. After the successful sync of the project, we will change the theme according to our NavigationView. So, we will create a style for our project in the styles.xml file. Here is the code of the styles.xml file.
Step 4. We will change the theme of the project in our manifest.xml file. So here is the code example of the manifest.xml file of the project.
Step 5. Now, we will create a new layout file named nav_header.xml, which will be the header of the NavigationView. So, here is the code of the nav_header.xml file.
Step 6. After creating the NavigationView header, we will create a menu file named drawer_menu to add the menu items for the NavigationView. Here is the code of the drawer_menu.xml file.
Here, we make two groups in our menu file. In the first group, we created three items: message, chat, and profile. In the second group, we create two items: name share and send. We add a title, id, and an icon for every item. For icons, we add some icons in our drawable file, and we can also use images as the icons; for this, we have to add the images in our project's drawable file.
Step 7. Now, we will create the Fragment Java classes and layouts for these fragments for every item that we add to our menu file. So when we select any item from this list, the Fragment of this item will be open, and as the home screen, we will open a Fragment named "Message". So here is the code of these Fragments.
Here is the layout file named fragment_message.xml file for the message item.
In this fragment layout file, we will show a message through the TextView that "Message is Selected". Now, let's look at the fragment Java class named MessageFragment.java.
Now, here is the layout file named fragment_chat.xml file for the Chat item.
In this fragment layout file, we will show a message through the TextView that "Chat is Selected". Now, let's look at the fragment Java class named ChatFragment.java.
Now, here is the layout file named fragment_profile.xml file for the profile item.
In this fragment layout file, we will show a message through the TextView that "Profile is Selected". Now, let's look at the fragment Java class named ProfileFragment.java.
Now, here is the layout file named fragment_share.xml file for the Share item.
In this fragment layout file, we will show a message. "Share," through the TextView. Now, let's look at the fragment Java class named ShareFragment.java.
Now, here is the layout file named fragment_send.xml file for the Send item.
In this fragment layout file, we will show a message, "Send," through the TextView. Now, let's look at the fragment Java class named SendFragment.java.
So, these are the fragment layouts and the Java Fragment files for all the items that we have created in our menu file.
Step 8. Now, we will create our NavigationView in our ativity_main.xml file. So, here is the code of activity_main.xml file.
In this activity_main.xml file, first, we create a Drawer and set the openDrawer as "start". In LinearLayout, we create FrameLayout and a toolbar for our navigationViewExample project. In the last, we create NavigationView and use nav_header as the header of the NavigationView and also include the menu items file named "drawer_menu".
Step 9. In the last, here is the MainActivity.java class file of our Android Studio project. The code of the MainActivity.java file is listed below.
Explanation
In the MainActivity.java file, first, we used the toolbar by the line "findViewById(R.id.toolbar)" and set the toolbar in our project by "setSupportSctionBar(toolbar)". We overrode a method named "public boolean onNavigationItemSelected(@NonNull MenuItem item)". To override this method, we need to implement "NavigationView.OnNavigationItemSelectedListener".
We use the switch statement as shown in the MainActivity.java class. As the default home screen of our project, we set the MessageFragment. So when we run our project, a "Message is Selected" message will be shown on the home screen of our project.
Here is the output of the NavigationViewExample.
![navigation view example output]()
Summary
In this article, we learned about NavigationView in Android with Java Programming Language with an example program in Android Studio.