Table of Contents
- 1 1. Introduction
- 2 2. Create the project for Bottom Navigation Bar with Activity
- 3 3. Setting the dependency Bottom Navigation Bar for Activity
- 4 4. Adding Bottom Navigation Bar for Activity to your XML
- 5 5. Create Bottom Navigation Bar Menu Item
- 6 5.1 Create Menu Folder
- 7 5.1 Create Menu resource File
- 8 6. Setting menu item to Bottom Navigation Bar
- 9 7. Detecting touch or click event to switch the View
- 10 8. Loading Fragment in Bottom Navigation Bar
- 11 9. Conclusion
As per material design guideline, bottom navigation bar should be used where we need to use 3 to 5 menu item. More than that menu item is not recommended for the bottom navigation bar. If you would like to know more about bottom navigation bar recommendation you can check on material design website here.
In this tutorial, we are going to create an app to show how to use and implement the Bottom Navigation Bar in Android. We will create two fragments with different kind of content and view which will be changed as you touch the tab from Bottom Navigation Bar.
So creating the project Go To File> New > New Project, Put the name of your project here, for example, we have take name as Bottom Navigation Bar Demo then click on Next and select the empty activity.
As I explained at the starting of this tutorial Bottom Navigation Bar added to support Library from version 25.0.0. So add the dependency in project build.gradle files and sync the project. So we can add the Bottom Navigation bar to our Activity, but for that, we need to add the follwing dependency to our project first.
compile 'com.android.support:appcompat-v7:25.2.0' compile 'com.android.support:design:25.2.0'
Before adding the Bottom Navigation Bar to our app first we will test our application to check whether the application is working or not. So just click on run button from Android Studio Navigation menu and select the emulator.
Now open your main activity XML files and add the Bottom Navigation Bar to your activity_main.xml file.
An important point to note here is that we have added it under Linear Layout and apart for that, I have added a Frame Layout also inside Linear layout so that we can use this later on to show our fragment. You can check the layout using the Android Layout editor by clicking on Design Button it will look like as given in the screenshots.
As you can see in the screenshots we have just added the Bottom Navigation Bar to our project, but there is no any item inside our Bottom Navigation Bar. So it is time to add the menu Item to it.
5.1 Create Menu Folder
5.1 Create Menu resource File
Now, similarly right-click on menu directory and select the Menu Resource Files put the name of your menu XML files as bottom_nav_menu_item.xml.
For adding the menu Item to add the below code to your XML files or you can use the Android Studio Design Tool to add the menu item to your menu resource files.
We have created the menu item in our last steps which contain two item Home and Gallery, but if we run our app this menu will now show in our app because we still not added the menu item to our Bottom Navigation Bar. Now we will add the menu item to the navigation menu.
For adding the menu item open the main_activity.xml files once again and add the below line to your Bottom Navigation Bar declaration section.
So, After this change, our full Bottom Navigation Bar declaration looks like as given below.
At this point, you may want to test your app whether your menu item is showing or not, so hit the run button in from your Android Studio Menu bar and check out the application layout, if everything works well and implemented correctly then you will see two menu item inside your Bottom navBar menu one is Home and Gallery are visible.
7. Detecting touch or click event to switch the View
As of now, we have not done anything related to coding part of Bottom Navigation Bar implementation. So now its time to do some coding to see the real implementation of Bottom Navigation Bar. We need to modify our MainActivity.java file similar as given below to see the Bottom Navigation Bar in action.
Test your app again and check by tapping on both bottom menu item. You will see that top title bar heading is getting changed as you change the view. So we have completed the implementation part. Next, we will integrate the fragment with these menu items so that View can also be changed as you tab on the bottom navigation bar menu item.
Loading the Fragment on Item click is very important because this is the real use of the menu. Because you want to show different layout to your user when they tab on home and Gallery menu Item.
So for adding the fragment we need to create two fragments and attach that fragment with item selected listener. Below is the fragment that we are creating to use with home and gallery item of our Bottom Navigation Bar.
In a similar way as we created our Home Fragment we can create our Gallery Fragment. It’s time to bind the fragment with click event of the menu item. For this, we are adding two methods to our MainActivity.java files.
Now, we need to call these two methods from the respective case option of our Bottom Navigation Bar listener. Which will change the layout when user tab on the respective menu item of Navigation Bar.
We are all done now test your application and you see that your view is getting changed as you click on Bottom Navigation Menu.
We have created a fully functioning application with Bottom Navigation Bar. It is quite easy to implement and gives good UX to our app user.
In short, this is up to us how we are using this in our application. But I recommend following the Bottom Navigation Bar material design guidelines to use this in your mobile application. As per that it is good to use this if we have three to five menu item to show at the bottom.
You can check out the other option and function on Android Developer Official Sites.