The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.


Publishing Slices

In 2018, Google introduced the concept of slices, as a way for apps to publish information to be embedded in the UI of other apps. Google is expected to make a major push for apps to publish slices. This chapter explores how this is done.

What’s a Slice?

What a slice actually is depends on your perspective.

From the user’s standpoint, when they are in another app — such as Google Assistant – as part of their use of that app, they see data and UI elements that are tied to your app. So, for example, if your app helps users book hotel rooms, and the user asks the Assistant about a particular city, the Assistant might ask your app for a slice, to show the user hotel options in that city.

From the standpoint of your app, a slice is structured data, roughly analogous to publishing JSON from a Web service. While you get to specify text and images, and while you get to suggest particular types of user interactions (e.g., “allow the user to toggle this value between on and off”), the actual UI is rendered by another app. In some respects, this is reminiscent of app widgets, but RemoteViews give you much more direct control over the actual UI than do slices.

What Slices Contain

The data that makes up a slice is a combination of a generic UI structure and the user-facing elements (e.g., text, images) that should go into that UI structure. So, for example, you can create a list or grid, by saying that you want a series of rows and providing the text and images to go into those rows.

Elements in that UI structure can also be associated with PendingIntent objects to let you know when the user interacts with them, from simple clicks on image buttons, to being notified when the user changes values of toggles and sliders.

However, while you indicate in your code that you want a grid, the actual details of what that grid really looks like (margins, padding, colors, etc.) is up to the app that displays your slice. You do not get a vote.

Where Slices Get Used

In many respects, this is the big mystery at the moment.

Google has indicated that some of their products — Search and Assistant — will support slices. Google has also published code to allow other apps to display slices. However, as with Google’s code for allowing apps to host app widgets, the code is largely undocumented. It remains to be seen how widespread slices become. They could be huge, or they could be a little-used feature — only time will tell.

A Tale of Two Slices

The preview of this section is out seeking fame and fortune as the Dread Pirate Roberts.

Slice Sizes

The preview of this section is in an invisible, microscopic font.

Setting Up a Slice

The preview of this section left for Hollywood to appear in a reality TV show.

Binding a Slice

The preview of this section was last seen in the Bermuda Triangle.

Trying a Slice

The preview of this section is [REDACTED].

The User Flow

The preview of this section was whisked away by a shark-infested tornado.

The SliceAction

The preview of this section is in the process of being translated from its native Klingon.

The Slice Item Templates

The preview of this section was lost due to a rupture in the space-time continuum.

Actions and Sizes

The preview of this section was fed to a gremlin, after midnight.

Asynchronous Slices

The preview of this section took that left turn at Albuquerque.

Warning: Make No Rendering Assumptions

The preview of this section is presently indisposed.

Other Slice Viewer Features

The preview of this section is [REDACTED].