Android Navigation Architecture Component with Bottom Navigation View Step by Step Implementation

  1. Add Navigation Architecture Component & Material Design Dependencies
//navigation architecture component
implementation “androidx.navigation:navigation-fragment-ktx:2.3.0”
implementation “androidx.navigation:navigation-ui-ktx:2.3.0”
// material design
implementation ‘com.google.android.material:material:1.2.0’

2. Create Menu Resource for Bottom Navigation View (res/menu/menu_bottom_sheet_main.xml)

<?xml version=”1.0" encoding=”utf-8"?>
<menu xmlns:android=”"
<item
android:id=”@+id/nav_personal”
android:icon=”
android:title=”Personal” />
<item
android:id=”@+id/nav_transaction”
android:icon=”
android:title=”Transaction” />
<item
android:id=”@+id/nav_promotional”
android:icon=”
android:title=”Promotional” />
<item
android:id=”@+id/nav_blocked”
android:icon=”
android:title=”Blocked” />
</menu>

3. Add BottomNavigationView to Activity XML (activity_main.xml)

<?xml version=”1.0" encoding=”utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android=”"
xmlns:app=”"
xmlns:tools=”"
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”.MainActivity”>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id=”@+id/main_bottom_navigation_view”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
app:labelVisibilityMode=”labeled”
app:layout_constraintBottom_toBottomOf=”parent”
app:layout_constraintEnd_toEndOf=”parent”
app:layout_constraintStart_toStartOf=”parent”
app:menu=”” />
</androidx.constraintlayout.widget.ConstraintLayout>

4. Create Fragment for each of the menu items

PersonalFragment
TransactionFragment
PromotionalFragment
BlockedFragment

5. Create a new Navigation resource file (res/navigation/nav_manin.xml)

<?xml version=”1.0" encoding=”utf-8"?>
<navigation xmlns:android=”"
xmlns:app=”"
xmlns:tools=”"
android:id=”@+id/nav_main”
app:startDestination=””>
<fragment
android:id=”@+id/nav_personal” android:name=”com.jay.navigationarchdemo.ui.personal.PersonalFragment”
android:label=”personal_fragment”
tools:layout=”” />
<fragment
android:id=”@+id/nav_transaction” android:name=”com.jay.navigationarchdemo.ui.transaction.TransactionFragment”
android:label=”transaction_fragment”
tools:layout=”” />

<fragment
android:id=”@+id/nav_promotional”
android:name=”com.jay.navigationarchdemo.ui.promotional.PromotionalFragment”
android:label=”promotional_fragment”
tools:layout=”” />
<fragment
android:id=”@+id/nav_blocked”
android:name=”com.jay.navigationarchdemo.ui.blocked.BlockedFragment”
android:label=”blocked_fragment”
tools:layout=”” />
</navigation>

NOTE: make sure id of the fragment should match with the id specified in menu_bottom_sheet_main.xml

6. Add nav host fragment in activity XML (activity_main.xml)

<?xml version=”1.0" encoding=”utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android=”"
xmlns:app=”"
xmlns:tools=”"
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”.MainActivity”>
<fragment
android:id=”@+id/main_fragment_nav_host”
android:name=”androidx.navigation.fragment.NavHostFragment”
android:layout_width=”0dp”
android:layout_height=”0dp”
app:defaultNavHost=”true”
app:layout_constraintBottom_toTopOf=”ew”
app:layout_constraintEnd_toEndOf=”parent”
app:layout_constraintStart_toStartOf=”parent”
app:layout_constraintTop_toTopOf=”parent”
app:navGraph=”” />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id=”@+id/main_bottom_navigation_view”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
app:labelVisibilityMode=”labeled”
app:layout_constraintBottom_toBottomOf=”parent”
app:layout_constraintEnd_toEndOf=”parent”
app:layout_constraintStart_toStartOf=”parent”
app:menu=”” />
</androidx.constraintlayout.widget.ConstraintLayout>

7. Setup Bottom Navigation View with Nav Controller (MainActivity.kt)

package com.jay.navigationarchdemoimport android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.findNavController
import androidx.navigation.ui.setupWithNavController
import com.google.android.material.bottomnavigation.BottomNavigationView
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navController = findNavController(R.id.main_fragment_nav_host)
val bottomNavigationView = findViewById<BottomNavigationView>(R.id.main_bottom_navigation_view)
bottomNavigationView.setupWithNavController(navController)
}
}

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store