In this blog, we will learn how to integrate or work with Navigation Component In Android. Before starting our topic, we will discuss Navigation in brief.
Navigation refers to the interaction section of the application, that allows users to navigate from one page to another page throughout the application. With the help of Android’s JetPack, we can create Navigation from simple UI (Buttons) to complex UI (Bottom Navigation, Navigation Drawer).
In this blog, we will demonstrate the Navigation Component by creating Bottom Navigation in-app and will see how the navigation component works.
1. Adding dependecies
In this step, we will add all the dependencies related to the Navigation component in our “build.gradle” file. Here, we have added only the Kotlin related dependencies, if you are working on Java, then can add Java-related dependencies. You can take references of the Developer console
We can create the Navigation Graph class under the resource folder and select the Navigation under the Resource type drop-down list. We can add the action to each fragment, so their click event works accordingly, we can see the graph related to the Navigation XML file in the Android studio in the design section.
We will add following line of code to set the controller in main activity. Navigation component interanally deal with click events on the basis of the pages declared in the navigation Graph
1
2
3
val navController=findNavController(this,R.id.activity_main)
val bottomNavigationView=findViewById<BottomNavigationView>(R.id.activity_main)
Be the first to comment.