All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Custom Bottom Navigation Bar Android

Last Updated : Mar 11, 2024

Custom Bottom Navigation Bar Android

In this article we will show you the solution of custom bottom navigation bar android, top-level views are easily navigated using the bottom navigation bars. A quick way to navigate to an associated view is by tapping on the bottom navigation icon.

Users can navigate between top-level views with ease using the navigation bars at the bottom of the screen.

When a top-level destination list contains three to five items, they should be used.

Switching between activities or fragments is easy with the bottom navigation. A user's current location is highlighted in the bottom navigation.

The bottom navigation bar is a widget at the bottom of the app that allows users to select or navigate between different pages.

It is typically available with scaffolds, and used in conjunction with them.

  1. Switching between activities/fragments is easy with this tool.
  2. The app displays the different screens available to the user.
  3. It is possible for the user to check which screen they are currently on.

Step By Step Guide On Custom Bottom Navigation Bar Android :-

Theme Code

<style name=”AppTheme1” parent1=”Theme.MaterialComponents.Light.DarkActionBar”>
<resources>
    <style name="AppTheme1" parent1="Theme.MaterialComponents.Light.DarkActionBar">
        <item name="colorPrimary1">@color/colorPrimary</item>
        <item name="colorPrimaryDark1">@color/colorPrimaryDar1k</item>
        <item name="colorAccent1">@color/colorAccent1</item>
    </style>
</resources>

XML Code

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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_parent1"
    android:layout_height="match_parent1"
    tools:context=".Activity">
    <TextView
        android:id="@+id/firstFragment"
        android:layout_width="wrap_con"
        android:layout_height="wrap_con"
        android:text="TalkersCode"
        android:textColor="#43a047"
        android:textSize="50sp"
        android:textStyle="italic|bold"
        app:layout_constraintBottom_toBottomOf="parent1"
        app:layout_constraintEnd_toEndOf="parent1"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent1"
        app:layout_constraintTop_toTopOf="parent1" />
</androidx.constraintlayout.widget.ConstraintLayout>

XML Code

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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_parent1"
    android:layout_height="match_parent1"
    tools:context=".Activity">
    <TextView
        android:id="@+id/secondFragment"
        android:layout_width="wrap_con"
        android:layout_height="wrap_con"
        android:text="Data Structure"
        android:textColor="#43a047"
        android:textSize="50sp"
        android:textStyle="italic|bold"
        app:layout_constraintBottom_toBottomOf="parent1"
        app:layout_constraintEnd_toEndOf="parent1"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent1"
        app:layout_constraintTop_toTopOf="parent1" />
</androidx.constraintlayout.widget.ConstraintLayout>
  1. Android Studio allows the creation of new projects.
  2. The build.gradle(:app) file should be updated to import the Material Design Library. We will be using Android's Material Design Library in the build.gradle(:app) file. Adding the following dependency is necessary:
  3. This implementation implements the material 1.2.0 specification in com.google.android.material.
  4. Using activity main.xml, you must develop an app with such a Project will be based and a Bottom Navigation Buttons. Clicking the bottom nav bar will cause the FrameLayout's fragments to shift.
  5. There should be a menu in the bottom navigation bar. For the purpose of adding items to the bottom navigation bar, the menu will have to be used. It is essential to make a menu.
  6. Before you can construct a Menu, you must open the application's Recourses (correct) New Android Resources Directory -> Menu menu.
  7. Menu Resource Files can be created by clicking on the apps -> res -> menu (right-click) -> New -> Menu Resource File and naming it bottom_nav_menu.
  8. The bottom_nav_menu.xml file now allows the user to create as many items as he wishes. A new image asset can be created by clicking on the app -> res -> drawable (right-click) -> New -> Image Asset.
  9. An icon can be named whatever the user wants in the window that opens, but it should not contain any uppercase letters. When the user has found the icon he wants, he can select it by searching for it, then click Next-> Finish.
  10. The bottom_nav_menu.xml should now have these items. Below is an example of the bottom_nav_menu.xml file after it has been add.
  11. In order to prevent blackness on the Bottom Navigation Bar, we need to change the Action Bar's style to use the same material design library we are using. Change the style opening tag by navigating to app -> res -> values -> styles.xml .
  12. With our Bottom Navigation Bar now in place, we would like it to function by taking us to a different fragment/activity when an item is clicked. You can create fragments for each item in this example and navigate to them whenever the item corresponding to the fragment is clicked. Due to the fact that we created three items in the Bottom Navigation Bar, we will be creating three Fragments as well. The Fragment can be created by clicking on the app (right-click) -> New -> Fragment -> Fragment (Blank). Specify FirstFragment as the fragment name and fragment_first as the XML file name. There will be only one TextView in all three fragments to keep things simple.
  13. The next step is to code the FirstFragment to display fragment_first.xml. You will need to delete all the previous code in FirstFragment and replace it with the following code. We simply inflate the fragment layout we created above.
  14. After finishing the MainActivity file, we have everything we need. The last step is to code the MainActivity so it can connect everything to the application. Set the Fragment as an argument in the function setCurrentFragment() in the activity_main.xml file using the function setCurrentFragment().

Conclusion :-

Users can navigate between top-level views with ease using the navigation bars at the bottom of the screen.

When a top-level destination list contains three to five items, they should be used. Switching between activities or fragments is easy with the bottom navigation.

A user's current location is highlighted in the bottom navigation.

I hope this article on custom bottom navigation bar android helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪