How To Implement Bottom Navigation in Android Studio - Hive Programmers

in StemSocial11 months ago

Greetings to everyone and welcome to my favorite science community online, StemSocial.

In today's special episode of my series Android App Development Tutorials for beginners, I'm going to teach on how to implement bottom Navigation bar feature.

Polish_20240203_130946846.jpgOriginal Image Source by Firmbee from Pixabay

YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.png

The bottom navigation bar feature is a simple User Interface design that allows users of your App to navigate through different activity pages with just a click of one of the buttons in the bar.

This is a common UI element in most IOS Apps and it's pretty easy to use.

If you're building an App that happens to have more than three different activity pages in the Main activity page, I would recommend using bottom navigation instead of slider tab layout.

In this tutorial, I'll show you exactly how to implement the bottom navigation bar in your Android App. Let's get ready for today's work guys.

Prerequisites

For the benefit of newcomers to Android App development or my series, I'll share with you the required softwares you need to be able to go through with this tutorial.

  • Android Studio IDE - the platform on which we'll develop this and all other Android Apps.

  • Java Development Kit, JDK - You need to install JDK correctly on your PC for your PC to be able to execute Java commands or codes.

  • Physical Android Device or Emulator - After building the App, we'll need to test it either on an emulator or a physical Android device.

If you have all these checked out, you're absolutely ready to go through with this tutorial

2dk2RRM2dZ8gKjXsrozapsD83FxL3Xbyyi5LFttAhrXxr16mCe4arfLHNDdHCBmaJroMz2VbLrf6Rxy9uPQm7Ts7EnXL4nPiXSE5vJWSfR53VcqDUrQD87CZSpt2RKZcmrYrze8KanjkfyS8XmMCcz4p33NZmfHE4S9oRo3wU2.png

Create A new Android Project

We'll start building our project by creating a new Android Studio project. Just open the Android Studio IDE and click on "Create A New Project"

Although we could have easily selected the "Bottom Navigation" template I want us to learn the basic codes involved so we'll choose an "Empty Activity" template for simplicity and to also allow us to build the entire App UI from ground up.

Click on the "Next" button after and set both the App name and package name of your Android project. Also select Java as the programming language and click on the "Finish" button when you're through with the project configuration.

Android Studio will load your new Project Space.


Add Dependencies

Now guys since we'll be implementing the bottom navigation element, we would need to make sure that the "Material" library is integrated in our project.

So guys to do this, first open your app's build.gradle file and add the following dependencies for the Bottom Navigation.

Here's how the dependency code should look like

implementation 'com.google.android.material:material:1.4.0'

When you're through with that, click on the "Sync" button to sync your project and apply the changes.

YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.png

Design the Layout

The cool thing is that the material library comes equipped with a default Bottom navigation Element.

We don't have to create everything as a design, we can simply add the BottomNavigationView to our layout and we'll see the bottom navigation bar appear in our App at the bottom.

We'll be doing this inside our activity_main.xml layout file so open it and add the following code to create the Bottom Navigation.

Here's how the layout code should look like

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/fragmentContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottom_navigation_view"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_navigation_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:menu="@menu/bottom_navigation_menu"/>
</RelativeLayout>

YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.png

Create Menu Items

Now guys just like we did with the slider tab, we need to create a new menu XML file which would contain all the items that would be displayed inside our Bottom navigation.

We don't have to add those items in the activity_main layout.

So guys go ahead and create a new XML resource file and name it bottom_navigation_menu.xml .

Please remember to create this file in the res/menu folder and not anywhere else.

So we can now add the menu items for your Bottom Navigation.

Here's how the code will look like for adding items to the bottom navigation bar

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/navigation_home"
        android:title="Home"
        android:icon="@drawable/ic_home"/>
    
    <item
        android:id="@+id/navigation_dashboard"
        android:title="Dashboard"
        android:icon="@drawable/ic_dashboard"/>
    
    <item
        android:id="@+id/navigation_notifications"
        android:title="Notifications"
        android:icon="@drawable/ic_notifications"/>
</menu>

YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.png

Create Fragments

Now guys, since the bottom navigation bar allows user to navigate through different pages in the same single page, we would need to create fragment pages for each page that the user would like to navigate to through the bottom navigation bar.

For this tutorial example I'll only create three fragments and call them HomeFragment, DashboardFragment and lastly NotificationsFragment to represent each Bottom Navigation tab.

So when we click any of these tabs, it would have to go there.

I will not be showing the codes for each fragment so you'll probably have to DIY, do it yourself guys🥰.

If you're having troubles with it, you can refer to my previous blog on creating fragments or you can comment down below for support from me.

YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.png

Implement Fragment Navigation

  • We're going to handle the final step now guys

In your MainActivity.java, set up the fragment navigation and this should be done within the onCreate method.

Here's how your code should look like guys

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation_view);

        bottomNavigationView.setOnNavigationItemSelectedListener(item -> {
            Fragment selectedFragment = null;

            switch (item.getItemId()) {
                case R.id.navigation_home:
                    selectedFragment = new HomeFragment();
                    break;

                case R.id.navigation_dashboard:
                    selectedFragment = new DashboardFragment();
                    break;

                case R.id.navigation_notifications:
                    selectedFragment = new NotificationsFragment();
                    break;
            }

            getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, selectedFragment).commit();
            return true;
        });

        // Set default selected fragment
        getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, new HomeFragment()).commit();
    }
}

2dk2RRM2dZ8gKjXsrozapsD83FxL3Xbyyi5LFttAhrXxr16mCe4arfLHNDdHCBmaJroMz2VbLrf6Rxy9uPQm7Ts7EnXL4nPiXSE5vJWSfR53VcqDUrQD87CZSpt2RKZcmrYrze8KanjkfyS8XmMCcz4p33NZmfHE4S9oRo3wU2.png

Run Your App

Congratulations guys, you have been able to implement bottom navigation.

It's time to now run your app on an emulator or a physical Android device. When you App launches, you should see a Bottom Navigation layout with Home, Dashboard, and Notifications tabs.

Thank you so much for the time guys. I hope you enjoyed this episode as always.

Thank you all for the support. To my friends @stickupcurator or @stickupboys and everyone else

Have A Great Day And Catch You Next Time On StemSocial. Goodbye 👨‍💻


You Can Follow Me @skyehi For More Like This And Others

Sort:  

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

Thanks for including @stemsocial as a beneficiary, which gives you stronger support.