Android Jetpack: Android Slices (Part-1) Introduction

Saurabh Patel
ProAndroidDev
Published in
5 min readJun 18, 2018

Android Slices Part-1

Android Slices Part-2 (Android Slices Sample App)

What’s Android Slice:

Slices are Android’s new approach to show the remote content. In very simple language Slice is the UI component that can display the contents from your application from within the Google Search app and later in other places like the Google Assistant or Google Assistant devices. Slices work better with App actions.

Overall, Slices are

  • Templated: You can create Flexible, rich and dynamic UI (Slice) template to showcase your application contents with your application theme and colors.
  • Interactive: You can interact with it and take different actions on it. For eg. deep-link to the application, update data, take inline actions, scroll or toggle the UI component.
  • Updatable: It’s easy to update and render at different places. Currently, it supports from Google App search bar but it will be available on the google assistant surface or any other android based visual device.

Most important thing is, Android Slices are backward compatible through KitKat/API-19+ (95% of the android devices)

What’re the Use-Cases

In the following image, Let’s see some of the examples as well.

  1. Navigation Use-Case: The users are searching something which they already know they want to get to. In this example, the user is just typing get a ride keywords in the search bar and Lyft application is using current real-time data of the user and shows the best possible actions which user can take with the help of Slices.
  2. Interactive Action Use-case: Settings app provides the interactive action on the mobile data settings and the user can sense the other mobile data related information (for eg. Used Data since when).
Image from Google IO’18 Session

3. Recall, Search & Discovery Use-case: Slices can be used to recall, search or discover your app contents when a user is looking for something similar or contextual. For eg., the user has bookmarked a python course in your learning application and a user is trying to search python in the google app search bar at that time your learning application can show the slice with the course card and ask you to start/resume the course by providing inline action.

Slices Architecture

Android Slices are constructed on the top of content providers. Actually, SliceProvider is the extension version of Content Provider, check here for the more info. Moreover, slices are based on the content URIs, you can host variety of slices from your application. Your application will get a content URI and app can decide what kind of Slice needs to build and present to the user.

You can see the high level architecture in the below image, here onBlindSlice() gets called when app wants to show the slice and it returns the slice based on the input content URI. You can also update the slice with help of notifyChange().

Image from Google IO’18 Session

Let’s code, too much talk!! Code Android P Slice 🍕 😃

Prerequisites

  • AndroidX is required, so start the new project or use an existing one that has been refactored to AndroidX.
  • Android Studio 3.2 or later which provides AndroidX refactoring tool, Slices lint checks and SliceProvider template.
  • To test the Slice: Download and install the SliceViewer tool APK, download it from here and install it using the below command. SliceViewer tool displays the slices based on the content URI and you change the mode of slice as well.
adb install -r -t slice-viewer.apk

Let’s build the sample slice

  1. Start the new android project and add the SliceProvider class in one of your application packages. To add the SliceProvider class, right click on the application package-> New-> Other-> SliceProvider, you can give any name to this class and fill the other necessary details. You can give authority name, uri name and select the coding language. It will generate the SliceProvider class and modify the AndroidManifest.xml file. However, you can add SliceProvider class manually and extends it from SliceProvider, then you can add provider info in AndroidManifest file.
<!-- To provide slices you must define a slice provider -->
<provider
android:name=".MySliceProvider"
android:authorities="com.saurabh.androidslices"
android:exported="true"
>
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.app.slice.category.SLICE"/>

</intent-filter>
</provider>

2. If you add SliceProvider from Android Studio then it will add the dependencies automatically, otherwise, you have to add slice-core and slice-builders dependencies

implementation 'androidx.slice:slice-builders:1.0.0-alpha3'
implementation 'androidx.annotation:annotation:1.0.0-alpha3'

3. Back to the SliceProviderClass which you’ve just added, You can see there are two abstract methods which you have to override in the subClass of SliceProvider. onCreateSliceProvider() and onBindSlice(). Each Slice has an associated URI. When a surface wants to display a Slice, it sends a binding request to your app with this URI. Your app then handles this request and dynamically builds the Slice via the onBindSlice method. The surface can then display the Slice when appropriate. Below code snippet just show very simple slice with one row which contains the title only.

override fun onCreateSliceProvider(): Boolean {
return true
}

override fun onBindSlice(sliceUri: Uri): Slice? {

val slice: Slice;
if (sliceUri.path == "/hello") {
slice = ListBuilder(context, sliceUri, ListBuilder.INFINITY)
.addRow { it.setTitle("URI found.") }
.build()
} else {
slice = ListBuilder(context, sliceUri, ListBuilder.INFINITY)
.addRow {
it
.setTitle("URI not found.")
}
.build()
}
return slice;
}

4. Now, how to test/display this slice? You already have SliceViewer tool installed in the device/emulator. The Second step is: install your android application via command line or Android Studio whichever you prefer. Now, you have both apks installed on your test device/emulator. Run the following command to display the Slice. It’s the normal command to start the action intent with data URI. After running this command you can see your slice on SliceViewer screen. If it’s asking for the permission then give it because slice viewer tool requests the necessary permission to your application’s slice provider.

adb shell am start -a android.intent.action.VIEW -d slice-<content_uri_name>for eg. "content://com.saurabh.androidslices/hello"

There is a way to launch the slice viewer from the android studio as well, you can refer this section if you want to learn more about it.

Sample Code:

That’s it !! I don’t want to write too long in the first part of Android Slices. I’ll soon add the second part where I’ll explain all different kinds of slices with code. Stay tuned !!

Thank you for using your precious time to reading this article, If you liked the article, Clap your 👏👏 to say “thanks!” and help others find this article by sharing it. Also, you can follow me on Twitter or Medium!! #KeepLearning #AlwaysLearning📚

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Published in ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Responses (2)

What are your thoughts?