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.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Android Slices: Getting Started
20 mins
- Getting Started
- Slice Viewer
- Displaying Your Slice With Slice Viewers
- Installing the Slice Viewer App
- Creating the BookstoreSliceProvider Class
- Implementing the SliceProvider Class
- Creating the Bookstore Slice
- Slice Builders
- Letting the Slice Viewer Show Your Slice
- Using GridRowBuilder to Display Books
- Adding a Notification Icon
- Setting the Accent Color
- Display Modes
- Where to Go From Here?
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:
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:
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:
And with that, you have finished your Slices tutorial!
Where to Go From Here?
You can download the completed project using the Download Materials button at the top or bottom of this tutorial.
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!