BottomNavigationView makes it easy for users to explore and switch between top-level views in a single tap. There should be a minimum of 3 top-level views and a maximum of 5. If Destinations are more than 5 then use the Navigation Drawer. When the user taps on the icon it will change the top-level view accordingly. In a Music Player app to switch between Home, Album, and Radio, it can be used. Google plus app uses this widget to switch between different views. Instagram uses BottomNavigationView to switch between Feeds, Search, add, Like, and Profile. This is how a BottomNavigationView looks like.
Some Advantages and Disadvantages of BottomNavigationView are:
- It is Top-level destinations that can be accessed from anywhere in the app.
- It is a Material Design Component.
- Easy to use and implement.
- It is used only when we have only three to five Destinations.
- Can only be used with Mobile and Tablets.
- Length of Text Labels should be less.
- It should be used when the user will spend more than 90% of the time in an app in the same window.
- Using with TabLayout may cause confusion to the user.
- Add the support library in the build.gradle file and add a dependency in the dependencies section. This library has the inbuilt widget for the Bottom Navigation view so through this library it can be directly added.
- Now create a new Android Resource Directory. Right-click on the res folder and select Android Resource Directory. Make sure to select the resource type as a menu. Now create the bottom_menu.xml file and add the following code. In this file, we add the title, id, and icon of our menu for BottomNavigationView. Below is the code for the bottom_menu.xml file.
- Create an AlgorithmFragment by right click on the java package, select new -> Fragment(Blank).
- Follow the above steps for CourseFragment and LoginFragment.
- Now add the following code in the AlgorithmFragment.xml file. Here a TextView is added to the layout.
- Below is the code for the fragment_algorithm.xml file.
- Now add the following code in the CourseFragment.xml file. Here a TextView is added to the layout.
- Below is the code for the fragment_course.xml file.
- Now add the following code in the fragment_profile.xml file. Here a TextView is added to the layout.
- Below is the code for the fragment_profile.xml file.
- Now add the following code in the activity_main.xml file. In this file, we add BottomNavigationView to our layout.
- Now add the following code in the MainActivity.java file. In this file, we add OnNavigationItemSelectedListener which helps to navigate between the fragments. It will switch the fragment when the user taps on the icon.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.