www

How to Code a Bottom Navigation Bar for an Android App

Final product image
What You’ll Be Creating

The material design team at Google defines the functionality of bottom navigation bars in Android as follows:

Bottom navigation bars make it easy to explore and switch between top-level views in a single tap.

Tapping on a bottom navigation icon takes you directly to the associated view or refreshes the currently active view.

According to the official material design guidelines for the bottom navigation bar, it should be used when your app has:

  • three to five top-level destinations
  • destinations requiring direct access

An example of a popular app that implements the bottom navigation bar is the Google+ Android app from Google, which uses it to navigate to different destinations of the app. You can see this yourself by downloading the Google+ app from Google Play store (if you don’t already have it on your device). The following screenshot is from the Google+ app displaying a bottom navigation bar. 

Screenshot of Google Android app

In this post, you’ll learn how to display menu items inside a bottom navigation bar in Android. We’ll use the BottomNavigationView API to perform the task. For an additional bonus, you’ll also learn how to use the Android Studio templates feature to quickly bootstrap your project with a bottom navigation bar. 

A sample project (in Kotlin) for this tutorial can be found on our GitHub repo so you can easily follow along.

Prerequisites

To be able to follow this tutorial, you’ll need:

  • Android Studio 3.0 or higher
  • Kotlin plugin 1.1.51 or higher

1. Create an Android Studio Project

Fire up Android Studio and create a new project (you can name it BottomNavigationDemo) with an empty activity called MainActivity. Make sure to also check the Include Kotlin support check box. 

Create Android Project dialog

2. Adding the BottomNavigationView

To begin using BottomNavigationView in your project, make sure you import the design support and also the Android support artifact. Add these to your module’s build.gradle file to import them. 

Also, visit your res/layout/activlty_main.xml file to include the BottomNavigationView widget. This layout file also includes a ConstraintLayout and a FrameLayout. Note that the FrameLayout will serve as a container or placeholder for the different fragments that will be placed on it anytime a menu item is clicked in the bottom navigation bar. (We’ll get to that shortly.)  

Here we have created a BottomNavigationView widget with the id navigationView. The official documentation says that:

BottomNavigationView represents a standard bottom navigation bar for application. It is an implementation of material design bottom navigation.

Bottom navigation bars make it easy for users to explore and switch between top-level views in a single tap. It should be used when application has three to five top-level destinations.

The important attributes you should take note of that were added to our BottomNavigationView are:

  • app:itemBackground: this attribute sets the background of our menu items to the given resource. In our case, we just gave it a background colour. 
  • app:itemIconTint: sets the tint which is applied to our menu items’ icons.
  • app:itemTextColor: sets the colours to use for the different states (normal, selected, focused, etc.) of the menu item text.

To include the menu items for the bottom navigation bar, we can use the attribute app:menu with a value that points to a menu resource file. 

Here is the res/menu/navigation.xml menu resource file:

Here we have defined a Menu using the 

 which serves as a container for menu items. An  creates a MenuItem, which represents a single item in a menu. As you can see, each  has an id, an icon, and a title.

3. Initialization of Components

Next, we are going to initialize an instance of BottomNavigationView. Initialization is going to happen inside onCreate() in MainActivity.kt.

4. Testing the App

Now we can run the app!

First demo app showing bottom navigation

As you can see, our bottom navigation bar is showing at the bottom of the app screen. Nothing will happen if you click on any of the navigation items there—we’re going to handle that part in the next section. 

5. Configuring Click Events

Now, let’s see how to configure click events for each of the items in the bottom navigation bar. Remember that clicking on any item in there should take the user to a new destination in the app.

Here we called the method setOnNavigationItemSelectedListener. Here is what it does according to the official documentation:

Set a listener that will be notified when a bottom navigation item is selected.

We used the when expression to perform different actions based on the menu item that was clicked—the menu item ids serve as constants for the when expression. At the end of each when branch, we return true.

We then pass our mOnNavigationItemSelectedListener listener to setOnNavigationItemSelectedListener() as an argument. 

Be aware that there is another similar method called setOnNavigationItemReselectedListener, which will be notified when the currently selected bottom navigation item is reselected.

Next, we are going to create the different pages (or Fragments) for each of the menu items in the navigation drawer so that when a menu item is clicked or tapped, it displays a different Android Fragment or page. 

6. Creating the Fragments (Pages)

We’ll start with the SongsFragment.kt class, and you should follow a similar process for the remaining two fragment classes—AlbumsFragment.kt and ArtistsFragment.kt.

Here is my SongsFragment.kt:

Also, here is my R.layout.fragment_songs

7. Launching the Fragments

When any of the menu items is clicked, we open the corresponding Fragment and also change the action bar title. 

Here we’re using a method called openFragment() that simply uses the FragmentTransaction to add our fragment to the UI. 

Now run the project again to see how it all works!

Final app demo with click events

Anytime you click on any menu item, it will take the user to a new Fragment. 

Note that when we have more than four menu items in the bottom navigation bar—i.e. in BottomNavigationView—then the Android system automatically enables shift mode. In this mode, when any of the menu items is clicked, the other items on the right or left of the clicked item are shifted. 

BottomNavigationView with shift mode

8. Bonus: Using Android Studio Templates

Now that you have learnt about the APIs involved to create a bottom navigation bar from scratch in Android, I’ll show you a shortcut that will make it faster next time. You can simply use a template instead of coding a navigation bar from scratch. 

Android Studio provides code templates that follow the Android design and development best practices. These existing code templates (available in Java and Kotlin) can help you quickly kick-start your project. One such template can be used to create a bottom navigation bar. 

To use this handy feature for a new project, first fire up Android Studio. 

Create Android Project dialog

Enter the application name and click the Next button. You can leave the defaults as they are in the Target Android Devices dialog. 

Click the Next button again. 

Add an Activity to Mobile dialog

In the Add an Activity to Mobile dialog, select Bottom Navigation Activity. Click the Next button again after that. 

Configure Activity dialog

In the last dialog, you can rename the Activity, or change its layout name or title if you want. Finally, click the Finish button to accept all configurations. 

Android Studio has now helped us to create a project with a bottom navigation activity. Really cool!

You are strongly advised to explore the code generated. 

In an existing Android Studio project, to use this template, simply go to File > New > Activity > Bottom Navigation Activity.

Navigation from file menu to Bottom Navigation Activity

Note that the templates that come included with Android Studio are good for simple layouts and making basic apps, but if you want to really kick-start your app, you might consider some of the app templates available from Envato Market. 

They’re a huge time saver for experienced developers, helping them to cut through the slog of creating an app from scratch and focus their talents instead on the unique and customised parts of creating a new app.

Conclusion

In this tutorial, you learned how to create a bottom navigation bar in Android using the BottomNavigationView API from scratch. We also explored how to easily and quickly use the Android Studio templates to create a bottom navigation activity. 

I highly recommend checking out the official material design guidelines for bottom navigation bar to learn more about how to properly design and use the bottom navigation bar in Android.   

To learn more about coding for Android, check out some of our other courses and tutorials here on Envato Tuts+!

  • Android SDK
    Build a Music App With an Android App Template
    Chike Mgbemena
  • App Templates
    15 Best Android App Templates of 2017
    Nona Blackman
  • Android SDK
    Android From Scratch: Using REST APIs
    Ashraff Hathibelagal
  • Android SDK
    How to Code a Navigation Drawer for an Android App
    Chike Mgbemena
  • Kotlin
    Kotlin From Scratch: More Fun With Functions
    Chike Mgbemena

Powered by WPeMatico

Leave a Comment

Scroll to Top