Update UI Components With NavigationUI
Update UI Components With NavigationUI
The Navigation component includes a NavigationUI class. This class contains static methods that manage navigation with the top
app bar, the navigation drawer, and bottom navigation.
NavigationUI contains methods that automatically update content in your top app bar as users navigate through your app. For
example, NavigationUI uses the destination labels from your navigation graph to keep the title of the top app bar up-to-date.
<navigation>
<fragment ...
android:label="Page title">
...
</fragment>
</navigation>
When using NavigationUI with the top app bar implementations discussed below, the label you attach to destinations can be
automatically populated from the arguments provided to the destination by using the format of {argName} in your label.
NavigationUI provides support for the following top app bar types:
Toolbar
CollapsingToolbarLayout
ActionBar
For more information on app bars, see Set up the app bar.
AppBarConfiguration
NavigationUI uses an AppBarConfiguration object to manage the behavior of the Navigation button in the upper-left corner of
your app's display area. The Navigation button’s behavior changes depending on whether the user is at a top-level destination.
A top-level destination is the root, or highest level destination, in a set of hierarchically-related destinations. Top-level destinations
do not display an Up button in the top app bar because there is no higher level destination. By default, the start destination of your
app is the only top-level destination.
When the user is at a top-level destination, the Navigation button becomes a drawer icon if the destination uses
a DrawerLayout. If the destination doesn't use a DrawerLayout, the Navigation button is hidden. When the user is on any other
destination, the Navigation button appears as an Up button . To configure the Navigation button using only the
start destination as the top-level destination, create an AppBarConfiguration object, and pass in the corresponding navigation
graph, as shown below:
KOTLINJAVA
In some cases, you might need to define multiple top-level destinations instead of using the default start destination. Using
a BottomNavigationView is a common use case for this, where you may have sibling screens that are not hierarchically related to
each other and may each have their own set of related destinations. For cases like these, you can instead pass a set of destination
IDs to the constructor, as shown below:
KOTLINJAVA
<LinearLayout>
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar" />
<fragment
android:id="@+id/nav_host_fragment"
... />
...
</LinearLayout>
Next, call setupWithNavController() from your main activity's onCreate() method, as shown in the following example:
KOTLINJAVA
...
Include CollapsingToolbarLayout
To include a CollapsingToolbarLayout with your Toolbar, first define the Toolbar and surrounding layout in your activity, as
shown below:
<LinearLayout>
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="@dimen/tall_toolbar_height">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleGravity="top"
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"/>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<fragment
android:id="@+id/nav_host_fragment"
... />
...
</LinearLayout>
Next, call setupWithNavController() from your main activity's onCreate method, as shown below:
KOTLINJAVA
...
Action bar
To add navigation support to the default action bar, call setupActionBarWithNavController() from your main
activity's onCreate() method, as shown below. Note that you need to declare your AppBarConfiguration outside of onCreate(),
since you also use it when overriding onSupportNavigateUp():
KOTLINJAVA
...
KOTLINJAVA
As an example, one of your destinations may use a standard Toolbar, while another uses an AppBarLayout to create a more
complex app bar with tabs, as shown in figure 2.
Figure 2. Two app bar variations. On the left, a standard Toolbar. On the right, an AppBarLayout with a Toolbar and tabs.
To implement this example within your destination fragments using NavigationUI, first define the app bar in each of your fragment
layouts, beginning with the destination fragment that uses a standard toolbar:
<LinearLayout>
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
... />
...
</LinearLayout>
Next, define the destination fragment that uses an app bar with tabs:
<LinearLayout>
<com.google.android.material.appbar.AppBarLayout
... />
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
... />
<com.google.android.material.tabs.TabLayout
... />
</com.google.android.material.appbar.AppBarLayout>
...
</LinearLayout>
The navigation configuration logic is the same for both of these fragments, except that you should
call setupWithNavController() from within each fragment's onViewCreated() method, instead of initializing them from the
activity:
KOTLINJAVA
view.findViewById<Toolbar>(R.id.toolbar)
.setupWithNavController(navController, appBarConfiguration)
}
Note: Placing the top app bar into the destination fragment layout will result in the app bar animating with the rest of the layout during fragment
transitions when a fragment transition is set.
As an example, the XML snippets below define a menu item and a destination with a common id, details_page_fragment:
...
<fragment android:id="@+id/details_page_fragment"
android:label="@string/details"
android:name="com.example.android.myapp.DetailsFragment" />
</navigation>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
...
<item
android:id="@id/details_page_fragment"
android:icon="@drawable/ic_details"
android:title="@string/details" />
</menu>
If your menu was added via the Activity's onCreateOptionsMenu(), for example, you can associate the menu items with
destinations by overriding the Activity's onOptionsItemSelected() to call onNavDestinationSelected(), as shown in the
following example:
KOTLINJAVA
drawer icon in the app bar or when the user swipes a finger from the left edge of the screen.
navigation menu.
The drawer icon is displayed on all top-level destinations that use a DrawerLayout.
To add a navigation drawer, first declare a DrawerLayout as the root view. Inside the DrawerLayout, add a layout for the main UI
content and another view that contains the contents of the navigation drawer.
For example, the following layout uses a DrawerLayout with two child views: a NavHostFragment to contain the main content and
a NavigationView for the contents of the navigation drawer.
<!-- Layout to contain contents of main body of screen (drawer will slide over this) -->
<fragment
android:name="androidx.navigation.fragment.NavHostFragment"
android:id="@+id/nav_host_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />
<!-- Container for contents of drawer - use NavigationView to make configuration easier -->
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true" />
</androidx.drawerlayout.widget.DrawerLayout>
Next, connect the DrawerLayout to your navigation graph by passing it to AppBarConfiguration, as shown in the following
example:
KOTLINJAVA
Note: When using NavigationUI, the top app bar helpers automatically transition between the drawer icon and the Up icon as the current
destination changes. You don't need to use ActionBarDrawerToggle.
Next, in your main activity class, call setupWithNavController() from your main activity's onCreate() method, as shown below:
KOTLINJAVA
...
Note: Setting up the navigation drawer requires that you also set up your navigation graph and menu xml as described in Tie destinations to
menu items.
Bottom navigation
NavigationUI can also handle bottom navigation. When a user selects a menu item,
the NavController calls onNavDestinationSelected() and automatically updates the selected item in the bottom navigation
bar.
Figure 4. A bottom navigation bar.
To create a bottom navigation bar in your app, first define the bar in your main activity, as shown below:
<LinearLayout>
...
<fragment
android:id="@+id/nav_host_fragment"
... />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_nav"
app:menu="@menu/menu_bottom_nav" />
</LinearLayout>
Next, in your main activity class, call setupWithNavController() from your main activity's onCreate() method, as shown below:
KOTLINJAVA
...
Note: Setting up bottom navigation requires that you also set up your navigation graph and menu xml as described in Tie destinations to menu
items.
For a comprehensive example that includes bottom navigation, see the Android Architecture Components Advanced Navigation
Sample on GitHub.
Listen for navigation events
Interacting with the NavController is the primary method for navigating between destinations. The NavController is responsible
for replacing the contents of the NavHost with the new destination. In many cases, UI elements—such as a top app bar or other
persistent navigation controls like a BottomNavigationBar—live outside of the NavHost and need to be updated as you navigate
between destinations.
NavController offers an OnDestinationChangedListener interface that is called when the NavController's current
destination or its arguments change. A new listener can be registered via the addOnDestinationChangedListener() method.
Note that when calling addOnDestinationChangedListener(), if the current destination exists, it's immediately sent to your
listener.
NavigationUI uses OnDestinationChangedListener to make these common UI components navigation-aware. Note, however,
that you can also use OnDestinationChangedListener on its own to make any custom UI or business logic aware of navigation
events.
As an example, you might have common UI elements that you intend to show in some areas of your app while hiding them in
others. Using your own OnDestinationChangedListener, you can selectively show or hide these UI elements based on the target
destination