Android design supports library that provides a Bottom Navigation Bar where is an important widget in the Android application. Is a simpler way to look between 3 to 5 screens, if you have more than 5 screens is better to use slide navigation drawer.

Mostly, used fragments in navigation view to load different screens based on the user selection the usage of Fragments with Navigation may lead to back stack issues sometimes. In this article, we will learn how to use a Bottom Navigation Bar for different activities, instead of using fragments.

1.Creating a new project and adding Bottom Navigation Activity to Layout

Keep the default name of activity as MainActivity

2.Customizing Navigation Drawer Menu

To customize your menu, edit the file navigation.xml, res/menu/ navigation.xml. In this case we have 3 menu items, so you can change the default icons pressing right click on drawable folder New> Vector Asset> Clip Art, now you are ready to assign your icon by android:icon=”@drawable/Your Icon”

3.Creating Screens for Navigation Menu using Activities

Create two java classes ActivityOne and ActivityTwo which extends AppCompatActivity, and two layout resource files activity_one.xml and activity_two.xml in layout folder accordingly. Now, at this stage we must declare the new two activities in manifest file, just put this two lines under to </activity> of .MainActivity

4.Edit layout files for each activity

activity_main.xml

For the other two layouts just copy the xml code of the main activity and make few changes to understand the difference. We made this to keep the BottomNavigationView to all screens and navigate between of them.

activity_one.xml

activity_two.xml

5.Switching Between Activities

Replace the pre-build java code with the below

MainActivity.java

ActivityOne.java

ActivityTwo.java

That’s it! Now just run your application and navigate between activities!

Author

Write A Comment