App Bar and Navigation
App Bar and Navigation
MOBILE APPLICATION
DEVELOPMENT
APP BAR AND NAVIGATION VIEW
Created and Updated by S. Ong
Adapted by Chiew T K
Semester 1, 2023/2024
IN PRACTICAL 6
INTRODUCTION
• We will use the same navigation graph and files to
work on three different navigation methods:
App Bar
Bottom Navigation Bar
Side Navigation Bar
APP BAR
5
APP BAR
COMPONENTS IN APP
BAR
• In most basic form (as we have in the past), what are
the information shown on the App Bar?
Activity’s Title
Overflow Menu
ACTIONBAR VS
TOOLBAR
• ActionBar vs ToolBar classes which one to use?
• ActionBar behaves differently depending on version
of Android applied.
• Thus, for consistency purposes, ToolBar (the support
library) might be the more suitable choice to use when
implementing App Bar.
<androidx.drawerlayout.widget.DrawerLayout
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/DLMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" Adding Toolbar to
android:orientation="vertical"> the Main Activity
layout file.
<androidx.appcompat.widget.Toolbar
android:id="@+id/TBMainAct"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize" />
[email protected]
9
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_nav_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="@menu/menu_bottom" />
</LinearLayout>
</androidx.drawerlayout.widget.DrawerLayout>
TOOLBAR IN ACTION
• Now, let’s make the toolbar visible (replace it with
the original appbar) by using the following codes in
MainActivity.java:
TOOLBAR IN ACTION
(BINDING)
• In the onCreate callback method in MainActivity.java,
you also want to bind the NavHostFragment with the
NavController.
• NavController is an object that manages the app
navigation within the NavHost.
ADD OPTIONS TO
OVERFLOW MENU
• Add the options in the bottom_menu.xml file to the
Overflow Menu:
ADD ACTION TO
OVERFLOW MENU
ADD UP ACTION ON
TOOLBAR
• Override the onSupportNavigateUp() method in
MainActivity.java to enable the up action in the
toolbar:
OUTPUT
• At this step, your output should look like this:
<androidx.drawerlayout.widget.DrawerLayout
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/DLMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="@+id/TBMainAct"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize" />
[email protected]
23
<androidx.fragment.app.FragmentContainerView
android:id="@+id/NHFMain" Adding Bottom
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
Navigation View to
android:layout_height="0dp" the Main Activity
android:layout_weight="1" layout file.
app:defaultNavHost="true"
app:navGraph="@navigation/nav_animal_lover" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_nav_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="@menu/menu_bottom" />
</LinearLayout>
</androidx.drawerlayout.widget.DrawerLayout>
OUTPUT
• Your bottom tab should be working now.
NAVIGATION DRAWER
27
NAVIGATION DRAWER
• Also known as hamburger menu (imagine the 3 lines
icon as an hamburger of bun-patty-bun).
• Side navigation to navigate within destinations
available in the app
• The side navigation can be permanently sit for larger
screen, OR
• It can be toggled to open or close
• It is also a good place to show the hierarchical
functions in your app (you can composite/group the
related functions)
SETUP NAVIGATION
DRAWER (1/2)
• In the onCreate method in MainActivity.java, include
the following codes:
SETUP NAVIGATION
DRAWER (2/2)
• Similar to the bottom navigation bar setup:
• Step 1: in the onCreate callback method in
MainActivity.java, call ”setupNavMenu” while sending
the navController to the method.
• Step 2: include the following method in
MainActivity.java
OUTPUT
• When you click on the hamburger icon, you should
see the following output:
REMINDER
• The Online Test 1 will be conducted during the Week
8 Lecture Period.
• Scope of the test: Week 1 – Week 7 contents
• Format of the test: MCQ.
• Duration: 1 hour