User Navigation
User Navigation
User Interaction
Android
AndroidDeveloper
DeveloperFundamentals V2 User
FundamentalsV2 Usernavigation
navigation Commons Attribution 4.0 International
Creative Commons Attribution 4.0 International License
1
License.
Creative Commons Attribution 4.0 International License
4.4 User navigation
2
License.
Creative Commons Attribution 4.0 International License
Contents
● Back navigation
● Hierarchical navigation
○ Up navigation
○ Descendant navigation
● Navigation drawer for descendant navigation
○ Lists and carousels for descendant navigation
○ Ancestral navigation
○ Lateral navigation
3
License.
Creative Commons Attribution 4.0 International License
Two forms of navigation
Back (temporal) navigation
● Provided by the device's Back button
● Controlled by the Android system back stack
4
License.
Creative Commons Attribution 4.0 International License
Back
Navigation
5
License.
Creative Commons Attribution 4.0 International License
Navigation through history of screens
1. Historys starts from Launcher
6
License.
Creative Commons Attribution 4.0 International License
Changing Back button behavior
● Android system manages the back stack and Back button
● If in doubt, don't change
● Only override, if necessary to satisfy user expectation
7
License.
Creative Commons Attribution 4.0 International License
Overriding onBackPressed()
@Override
public void onBackPressed() {
// Add the Back key handler here.
return;
}
8
License.
Creative Commons Attribution 4.0 International License
Hierarchical
Navigation
9
License.
Creative Commons Attribution 4.0 International License
Hierarchical navigation patterns
● Parent screen—Screen that enables navigation down to
child screens, such as home screen and main Activity
● Collection sibling—Screen enabling navigation to a
collection of child screens, such as a list of headlines
● Section sibling—Screen with content, such as a story
10
License.
Creative Commons Attribution 4.0 International License
Example of a screen hierarchy
1. Parent 1 News App
11
License.
Creative Commons Attribution 4.0 International License
Types of hierarchical navigation
● Descendant navigation
○ Down from a parent screen to one of its children
○ From a list of headlines—to a story summary—to a story
● Ancestral navigation
○ Up from a child or sibling screen to its parent
○ From a story summary back to the headlines
● Lateral navigation
○ From one sibling to another sibling
○ Swiping between tabbed views
This work is licensed under a Creative
Creative Commons Attribution 4.0 International License
12
License.
Creative Commons Attribution 4.0 International License
Descendant
Navigation
13
License.
Creative Commons Attribution 4.0 International License
Descendant navigation
Descendant navigation News App
14
License.
Creative Commons Attribution 4.0 International License
Master/detail flow
● Side-by side on tablets ● Multiple screens on phone
15
License.
Creative Commons Attribution 4.0 International License
Controls for descendant navigation
● Navigation drawer
● Buttons, image buttons on main screen
● Other clickable views with text and icons arranged in
horizontal or vertical rows, or as a grid
● List items on collection screens
16
License.
Creative Commons Attribution 4.0 International License
Navigation
Drawer
17
License.
Creative Commons Attribution 4.0 International License
Navigation drawer
Descendant navigation
1. Icon in app bar
2. Header
3. Menu items
18
License.
Creative Commons Attribution 4.0 International License
Layouts for for navigation drawer
Create layouts:
● A navigation drawer as the Activity layout root ViewGroup
● A navigation View for the drawer itself
● An app bar layout that includes room for a navigation icon button
● A content layout for the Activity that displays the navigation drawer
● A layout for the navigation drawer header
19
License.
Creative Commons Attribution 4.0 International License
Navigation drawer Activity layout
1. DrawerLayout is root view
2. CoordinatorLayout contains
app bar layout with a Toolbar
3. App content screen layout
3
20
License.
Creative Commons Attribution 4.0 International License
Steps to implement navigation drawer
21
License.
Creative Commons Attribution 4.0 International License
Other descendant navigation patterns
22
License.
Creative Commons Attribution 4.0 International License
Ancestral
Navigation
23
License.
Creative Commons Attribution 4.0 International License
Ancestral navigation (Up button)
● Enable user to go up from a section
or child screen to the parent
24
License.
Creative Commons Attribution 4.0 International License
Declare parent of child Activity—AndroidManifest
<activity android:name=".OrderActivity"
android:label="@string/title_activity_order"
android:parentActivityName="com.example.android.
optionsmenuorderactivity.MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>
</activity>
25
License.
Creative Commons Attribution 4.0 International License
Lateral
Navigation
26
License.
Creative Commons Attribution 4.0 International License
Tabs and swipes
Lateral navigation News App
27
License.
Creative Commons Attribution 4.0 International License
Benefits of using tabs and swipes
● A single, initially-selected tab—users
have access to content without further
navigation
● Navigate between related screens
without visiting parent
28
License.
Creative Commons Attribution 4.0 International License
Best practices with tabs
29
License.
Creative Commons Attribution 4.0 International License
Steps for implementing tabs
1. Define the tab layout using TabLayout
2. Implement a Fragment and its layout for each tab
3. Implement a PagerAdapter from FragmentPagerAdapter or
FragmentStatePagerAdapter
4. Create an instance of the tab layout
5. Use PagerAdapter to manage screens (each screen is a Fragment)
6. Set a listener to determine which tab is tapped
30
License.
Creative Commons Attribution 4.0 International License
Add tab layout below Toolbar
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/toolbar"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
31
License.
Creative Commons Attribution 4.0 International License
Add view pager below TabLayout
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="@id/tab_layout" />
32
License.
Creative Commons Attribution 4.0 International License
Create a tab layout in onCreate()
33
License.
Creative Commons Attribution 4.0 International License
Add the view pager in onCreate()
34
License.
Creative Commons Attribution 4.0 International License
Add the listener in onCreate()
viewPager.addOnPageChangeListener(
new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(
new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());}
@Override
public void onTabUnselected(TabLayout.Tab tab) {}
@Override
public void onTabReselected(TabLayout.Tab tab) {} });
This work is licensed under a Creative
Creative Commons Attribution 4.0 International License
35
License.
Creative Commons Attribution 4.0 International License
END