How to create a Bottom Navigation Bar in ...

How to create a Bottom Navigation Bar in Kotlin — Beginner

Apr 22, 2022
277

image

We’ve all encountered apps with a bottom navigation bar. Instagram, WhatsApp, and other prominent instances come to mind. Let’s look at how to create a functional Bottom Navigation Bar in an Android app in this tutorial. Here’s a look at a sample Bottom Navigation Bar:

image

Let’s start :)

Step by Step Implementation

Step 1 — Adding the required dependency

implementation ‘androidx.legacy:legacy-support-v4:1.0.0’ 
implementation ‘androidx.navigation:navigation-fragment-ktx:2.4.1’ 
implementation ‘androidx.navigation:navigation-ui-ktx:2.4.1’

Step 2- Creating your fragments

Make as many fragments as you want :) , In this sample , I have created 3 fragments.

Step 3- Creating a navigation graph

To add a navigation graph to your project, do the following:

  1. In the Project window, right-click on the res directory and select New > Android Resource File. The New Resource File dialog appears.

  2. Type a name in the File name field, such as “navgraph” or “navmain” or everything you want :)

  3. Select Navigation from the Resource type drop-down list, and then click OK.

<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="@+id/nav_main"
    app:startDestination="@id/firstFragment">

    <fragment
        android:id="@+id/firstFragment"
        android:name="com.ezatpanah.bottomnavigation_toutube.FirstFragment"
        android:label="fragment_first"
        tools:layout="@layout/fragment_first" />
    <fragment
        android:id="@+id/secondFragment"
        android:name="com.ezatpanah.bottomnavigation_toutube.SecondFragment"
        android:label="fragment_second"
        tools:layout="@layout/fragment_second" />
    <fragment
        android:id="@+id/thirdFragment"
        android:name="com.ezatpanah.bottomnavigation_toutube.ThirdFragment"
        android:label="fragment_third"
        tools:layout="@layout/fragment_third" />
</navigation>

Step 4- Creating a menu for the Bottom Navigation Bar

The Navigation Bar needs to have some items which will create using Menu. To create a Menu, first, create a Menu Directory by clicking on the app -> res(right-click) -> New -> Android Resource Directory and select Menu in the Resource Type.

then, To create a Menu Resource File , click on the app -> res -> menu(right-click) -> New -> Menu Resource File and name it bottomnavmenu.

we also need to create an icon for each of these items. To create an icon, click on the app -> res -> drawable(right-click) -> New -> Vector Asset.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/firstFragment"
        android:icon="@drawable/ic_baseline_filter_1_24"
        android:title="First" />
    <item
        android:id="@+id/secondFragment"
        android:icon="@drawable/ic_baseline_filter_2_24"
        android:title="Second" />
    <item
        android:id="@+id/thirdFragment"
        android:icon="@drawable/ic_baseline_filter_3_24"
        android:title="Third" />
</menu>

Step 5-Add a NavHost and Bottom Navigation to an activity

Next, define a host fragment in the main layout (main_activity.xml). A nav host fragment is an empty container where destinations are swapped in and out as a user navigates through your app. We will also add the BottomNavigationView in the main layout, as shown below.

<?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_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <fragment
        android:id="@+id/activity_main_nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/nav_main" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavigationView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/nav_menu"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Step 6-Add Nav Controller into MainActivity.kt

The last step is to add Nav Controller into MainActivity.kt .

The NavController is an object that manages app navigation within a NavHost. It coordinates the swapping of fragments as users move through your app. In the main activity class, call setupWithNavController() from your main activity's onCreate() method, as shown below.

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.navigation.NavController
import androidx.navigation.NavGraph
import androidx.navigation.Navigation
import androidx.navigation.ui.NavigationUI.setupWithNavController
import com.ezatpanah.bottomnavigation_toutube.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding
    private lateinit var navController: NavController

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding= ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        navController=Navigation.findNavController(this,R.id.activity_main_nav_host_fragment)
        setupWithNavController(binding.bottomNavigationView,navController)


    }
}

it’s Done :)

Github Source :

https://github.com/Ezatpanah/BottomNavigation-Youtube

Also, you can check my video about BottomNavigation in android on Youtube :

https://youtu.be/jj5Rd_GCQkY

Vous aimez cette publication ?

Achetez un café à Reyhaneh (AndroidGeek.co)

Plus de Reyhaneh (AndroidGeek.co)