Bottom Navigation Android Example Using Fragments
Bottom Navigation Android Example Using Fragments
Fragments
January 23, 2018 by Belal Khan 5 Comments
In Android, there are many options for making navigation easy in your application for the
user. Bottom Navigation Bar is one of them. Bottom Navigation Bar always stays at the
bottom of your application and provides navigation between the views of your
application. So in this Bottom Navigation Android Example, we will see how we combine
the Bottom Navigation and Fragments.
Contents [hide]
1 Android Bottom Navigation
2 Using Bottom Navigation in Android
3 Bottom Navigation Android Example
o 3.1 Defining Colors and Strings
o 3.2 Customizing Bottom Navigation View
o 3.3 Creating Fragments
3.3.1 Creating Layout Resource Files
3.3.2 Creating Fragments
o 3.4 Switching Fragments
4 Bottom Navigation Android Example Source Code
o 4.1 Sharing is Caring:
o 4.2 Related
I guess many of you are already aware with this component, or you might have seen this
thing in some applications. We use Bottom Navigation When we have at least 3 top level
navigation views.
#3 While creating an Android Project you can select Bottom Navigation Activity from the
template.
Bottom Navigation Android Activity from Templates
colors.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <resources>
3 <color name="colorPrimary">#278be3</color>
4 <color name="colorPrimaryDark">#0f5998</color>
5 <color name="colorAccent">#4075a2</color>
6 <color name="colorNavIcon">#dae9f6</color>
7 <color name="colorNavText">#01294b</color>
8 </resources>
strings.xml
1 <resources>
5 <string name="title_home">Home</string>
6 <string name="title_dashboard">Dashboard</string>
7 <string name="title_notifications">Notifications</string>
8 <string name="title_profile">Profile</string>
10 </resources>
activity_main.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 xmlns:app="http://schemas.android.com/apk/res-auto"
4 xmlns:tools="http://schemas.android.com/tools"
5 android:id="@+id/container"
6 android:layout_width="match_parent"
7 android:layout_height="match_parent"
8 tools:context="net.simplifiedcoding.bottomnavigationexample.MainActivity">
9
10 <FrameLayout
11 android:id="@+id/fragment_container"
12 android:layout_width="match_parent"
13 android:layout_height="match_parent"
14 android:layout_marginBottom="56dp"
15 android:text="@string/title_home"
16 app:layout_constraintLeft_toLeftOf="parent"
17 app:layout_constraintTop_toTopOf="parent" />
18
19 <android.support.design.widget.BottomNavigationView
20 android:id="@+id/navigation"
21 android:layout_width="0dp"
22 android:layout_height="wrap_content"
23 android:background="@color/colorPrimary"
24 app:itemIconTint="@color/colorNavIcon"
25 app:itemTextColor="@color/colorNavText"
26 app:layout_constraintBottom_toBottomOf="parent"
27 app:layout_constraintLeft_toLeftOf="parent"
28 app:layout_constraintRight_toRightOf="parent"
29 app:menu="@menu/navigation" />
30
31 </android.support.constraint.ConstraintLayout>
Now, if you created the project using Bottom Navigation Activity template, a menu file
named navigation.xml is created by default inside the menu folder. If it is not created you
can create it manually as well. Here we define all the menu items that we need to display in
the Bottom Navigation Bar.
navigation.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <menu xmlns:android="http://schemas.android.com/apk/res/android">
4 <item
5 android:id="@+id/navigation_home"
6 android:icon="@drawable/ic_home_black_24dp"
7 android:title="@string/title_home" />
9 <item
10 android:id="@+id/navigation_dashboard"
11 android:icon="@drawable/ic_dashboard_black_24dp"
12 android:title="@string/title_dashboard" />
13
14 <item
15 android:id="@+id/navigation_notifications"
16 android:icon="@drawable/ic_notifications_black_24dp"
17 android:title="@string/title_notifications" />
18
19 <item
20 android:id="@+id/navigation_profile"
21 android:icon="@drawable/ic_profile_black_24dp"
22 android:title="@string/title_profile" />
23
24 </menu>
So, the above codes will produce the following Layout for our Main Activity.
Bottom Navigation Android Activity
If you have used the predefined template for creating Bottom Navigation View, then
you will have some codes in the MainActivity.java by default. We will remove those
codes and change the MainActivity as shown below, so that we can learn everything.
MainActivity.java
Java
1 package net.simplifiedcoding.bottomnavigationexample;
3 import android.os.Bundle;
4 import android.support.v7.app.AppCompatActivity;
8 @Override
10 super.onCreate(savedInstanceState);
11 setContentView(R.layout.activity_main);
12 }
13
14 }
Creating Fragments
For each of the view we need to create a layout resource file and a fragment class to inflate
the view. As we have 4 different views to be switched we need to create 4 layout resource
files and 4 java classes. Every class and resource file is almost the same as we don’t have
anything other than a simple TextView in the screens.
fragment_home.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent">
6 <TextView
7 android:layout_width="wrap_content"
8 android:layout_height="wrap_content"
9 android:layout_centerHorizontal="true"
10 android:layout_centerVertical="true"
11 android:text="Home"
12 android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" />
13
14 </RelativeLayout>
Don’t forget to change the text value of the TextView in each file.
Creating Fragments
HomeFragment.java
Java
1 package net.simplifiedcoding.bottomnavigationexample;
3 import android.os.Bundle;
4 import android.support.annotation.Nullable;
5 import android.support.v4.app.Fragment;
6 import android.view.LayoutInflater;
7 import android.view.View;
8 import android.view.ViewGroup;
10 /**
12 */
13
15 @Nullable
16 @Override
18 savedInstanceState) {
24 }
Switching Fragments
Now we will switch the screens or fragments when the bottom navigation menu is
clicked. We also need to load some fragment initially which is HomeFragment in
this case.
First we will create a method to switch the fragment. I have created the following
method named loadFragment() which is taking Fragment is an object.
Java
1 private boolean loadFragment(Fragment fragment) {
2 //switching fragment
3 if (fragment != null) {
4 getSupportFragmentManager()
5 .beginTransaction()
6 .replace(R.id.fragment_container, fragment)
7 .commit();
8 return true;
9 }
10 return false;
11 }
We will call the above method inside onCreate() to load the default fragment on
starting.
Java
1 @Override
3 super.onCreate(savedInstanceState);
4 setContentView(R.layout.activity_main);
7 loadFragment(new HomeFragment());
11 navigation.setOnNavigationItemSelectedListener(this);
12 }
Java
//implement the interface OnNavigationItemSelectedListener in your activity class
1
public class MainActivity extends AppCompatActivity implements
2
BottomNavigationView.OnNavigationItemSelectedListener {
With the above interface we will get method, and it will be called whenever we will
tap on an option from the Bottom Navigation View.
Java
1 @Override
4 return true;
5 }
Java
1 @Override
5 switch (item.getItemId()) {
6 case R.id.navigation_home:
8 break;
10 case R.id.navigation_dashboard:
12 break;
13
14 case R.id.navigation_notifications:
16 break;
17
18 case R.id.navigation_profile:
20 break;
21 }
22
23 return loadFragment(fragment);
24 }
So, the final code that we have for the MainActivity.java is.
MainActivity.java
Java
1 package net.simplifiedcoding.bottomnavigationexample;
3 import android.os.Bundle;
4 import android.support.annotation.NonNull;
5 import android.support.design.widget.BottomNavigationView;
6 import android.support.v4.app.Fragment;
7 import android.support.v7.app.AppCompatActivity;
8 import android.view.MenuItem;
12 BottomNavigationView.OnNavigationItemSelectedListener {
13
14
15 @Override
17 super.onCreate(savedInstanceState);
18 setContentView(R.layout.activity_main);
19
21 loadFragment(new HomeFragment());
22
25 navigation.setOnNavigationItemSelectedListener(this);
26 }
27
28
29 @Override
30 public boolean onNavigationItemSelected(@NonNull MenuItem item) {
32
33 switch (item.getItemId()) {
34 case R.id.navigation_home:
36 break;
37
38 case R.id.navigation_dashboard:
40 break;
41
42 case R.id.navigation_notifications:
44 break;
45
46 case R.id.navigation_profile:
48 break;
49 }
50
51 return loadFragment(fragment);
52 }
53
55 //switching fragment
56 if (fragment != null) {
57 getSupportFragmentManager()
58 .beginTransaction()
59 .replace(R.id.fragment_container, fragment)
60 .commit();
61 return true;
62 }
63 return false;
64 }
Download is Locked
Please support us, use one of the buttons below to unlock the content.
like
+1 us
tweet
So that’s all for this Bottom Navigation Android Tutorial friends. I hope the post helped you
to learn about Bottom Navigation. If it was helpful to you, then please give me a SHARE.
Thank You 🙂