Android Slices: Getting Started

In this tutorial, you’ll learn how to use Slices, which allow you to embed parts of your app into viewers like the Google Search app and Google Assistant. By Ahmed Tarek.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 3 of 3 of this article. Click here to view the first page.

Setting the Accent Color

You can set an accent color to tint all of the tintable items within the ListBuilder such as:

  • The icon in the primary action, which will be displayed in the shortcut mode.
  • Any icons in your slice.
  • Widgets: Switch, SeekBar and ProgressBar.

Add the following line to your ListBuilder before .build():

.setAccentColor(ContextCompat.getColor(context, R.color.colorAccent))

Build and run again. Notice that the notification icon color is changed from the accent color of the Slice Viewer app to the accent color you have set:

Notification icon color

Display Modes

Slice viewers can display Slices in three display modes:

You’ve aleady made your Slice compatible with the three modes by adding the following a summary to the Slice header within the apply block. The summary will be shown in the small mode instead of the subtitle.

Note: If you have not set a header to your ListBuilder, the first row added to your ListBuilder is displayed instead.

Note: If you have not set a header to your ListBuilder, the first row added to your ListBuilder is displayed instead.

Build and run in the Slice Viewer app.

Put your Slice in Small mode by tapping on the right-top icon in the Slice Viewer app and select small; you will see a screen like this:

Small mode

Put your Slice in the Shortcut mode by tapping on the right-top icon in the Slice Viewer app and select shortcut; you will see a screen like this:

Shortcut mode

And with that, you have finished your Slices tutorial!

Congratulations

Where to Go From Here?

You can download the completed project using the Download Materials button at the top or bottom of this tutorial.

Note: You’ll need to complete the instructions in the Slice viewer and Let the Slice viewer show your Slice sections to be able to run the final project.

During this tutorial you learned how to:

  • Create a Slice for a bookstore app.
  • Show the Slice on the surface of the Slice Viewer app.
  • Make the Slice interactive by adding a notification icon to let the user enable or disable notification without opening the app.
  • Make the Slice compatible with the different display modes.
  • Update the Slice with new data.

While you have learned how to incorporate Slices, note that, as of the writing this tutorial, the official Android documentation states: “Slices will start appearing soon for users, but you can start building today.”

You may also find it helpful to learn about RangeBuilder, which you can read about here.

And since Slices are a way to enhance App Actions, you may want to know more about App Actions, which you can learn about here.

Feel free to share your feedback or ask any questions in the comments below or in the forums!