Google added Bottom Navigation View in android design support library in it’s version 25. In this tutorial, you will learn more about android bottom navigation view, what is it, how to use and implement it in any application etc.
Android Bottom Navigation View is another way (like Navigation View, Sliding TabBar, Fixed TabBar etc.) to quickly navigate between different top level view in a single tap in an android application. There are several points you need to keep in mind while implementing this view. We have categorised these points in 4 parts. They are Usage, Style, Behaviour and Specs. We will talk about it one by one.
Usage
Bottom navigation View is used to quickly navigate between top-level views of an application. It is primarily designed to be used in mobile application. However, larger devices like Desktop may achieve similar behaviour by using side navigation.
It should be used for :
a. 3 to 5 top level destinations of similar importance.
b. Destination that needs direct access from anywhere in the application.
If you have more than five top level destinations, you can provide access to destinations not covered in bottom navigation view in navigation drawer.
Style
This section covers how should bottom navigation-view look, what should be the color of text, etc. Icons and Text:
We represent bottom navigation actions as icon, so, we should use icons that can be related to the respective actions. For example, If we are showing home tab, then, use icon that represents home. For example, you can see the example in output shown above.
You can also style each action as below.
a. When the view is in focus, show text label and icon both.
b. When there are only three actions, show both icons and text labels at all times.
c. When there are more than three actions, Show icons when view is inactive. Otherwise, show icons and text label.
Color:
a. Tint the current bottom navigation action with the app’s primary color.
b. If navigation bar is coloured, the color of icons and text of current action should be either black or white.
Text Labels:
a. Provide short and simple name to the bottom navigation actions as it will neither truncate nor wrap.
Behaviour
a. Tapping on a bottom navigation icon redirects you to the related views or refreshes the current view. Each icon must have a fixed destination and may not open menus or pop-ups.
b. Pressing back button should not navigate between different bottom navigation views.
c. It can appear/disappear dynamically. If you are doing so, it should hide while scrolling downward and reveals it while scrolling upward.
Specs
Width of each action can be calculated using below formula
Width of action = Width of entire view / Number of actions.
In this tutorial, you will learn how to use one of the most important design support library – Android Bottom Navigation View Library. Let’s start the tutorial now.
1.1 Creating New Project
Follow steps written below to create a new project.
a. Goto File –> New –> New Project. Then, Write application name as BottomNavigationView and click next.
b. Select Minimum SDK 15 or more –> click next –> Select Empty Activity –> click next –> click finish.
If you have followed above process correctly, you will get a newly created project successfully.
1.2 Basic setup for Bottom Navigation View
Follow steps below to setup bottom navigation view.
1.2.1 Add gradle into app/build.gradle
You need to add design support library gradle into app/build.gradle file.
compile 'com.android.support:design:25.1.0'
1.3 Important Attributes detail
Bottom Navigation View has certain attributes that can be used to style this view. You can choose the color of each menu, you can set the color of the whole tab, you can style the text appearance of each tab etc. by using appropriate attributes. Some of such attributes are as listed below.
app:Background — Use this attribute to set the background color of the bottom navigation view. app:itemIconTint — Use this attribute to tint the icons in bottom navigation view. app:itemTextColor — Use this attribute to set the text color of the item in bottom navigation view. app:menu — Use this attribute to set the menu(that contains the item to be displayed in Navigation View) of the Bottom Navigation View.
You can also check other attributes in this link. You will also find some methods that can be used to style the bottom navigation view. All of them are listed in this link.
2.0 Creating menu to Show Bottom Navigation View
We use menu xml file to show items needed in bottom navigation view. According to guidelines, three to five items are recommended. So, make sure you follow the guidelines while creating the menu items. It should be between three to five. So, let’s create menu items for bottom navigation view.
Create menu folder in res folder(res/menu). Now, Create bottom_navigation.xml file into this(res/menu) folder.
In this tutorial, we are going to show only 3 menu items. So, Add 3 items in bottom_navigation.xml file. Finally, bottom_navigation.xml file would look like below.
Now, We have completed basic setup for Bottom Navigation View. So, We will start implementing bottom navigation view in our application. There are 2 sections in which we will see the implementation. First section describes how to Add Bottom Navigation View into xml file. Second section contains code to perform some operations on tab item selection programatically.
3.1 Add Bottom Navigation View into xml file
This is bottom navigation view. Add this code into main_activity.xml file.
In this tutorial, we are showing corresponding fragment on tab selection. Here, I will create only one fragment and rest you can create similarly.
3.3 Create Fragments to show on Tab Menu Item Selection
We are showing only three menu items in Bottom navigation View. So, I will create fragment related to one of them. Similarly, you create fragments for other tab. You can also modify UI as per your requirements in the fragment.
Create a Java file named HomeFragment in com.tutorialwing.bottomnavigationview. Also, create an xml file named home_fragment in res/layout folder. Modify the UI as per your need. Here, we are just showing sample. So, Finally, both of these files would look like below.
If you run the app, You will see the output as shown below. Notice that i have used drawable folders to change the colour or MenuItem on selection. By default, this feature is ON. So, you do not need extra code for these features.
Conclusion
Android Bottom Navigation View is one of the most useful support that can be used to navigate between top level view within an application. However, This should be used only when we have three to five top level view in our application. If you want, you can download the source code from link given above.
how do i make the bottom navigation bar appear/disappear dynamically
You can use bottomNavigationView.setVisibility(View.GONE or View.VISIBLE);
thanxs buddy helpful for me
Your welcome..
thank you for clean code!
and comments!
Thanks!!!
ur welcome…