Introduction to Slices

Overview

What happens when your work is completed in one click instead of two or more?. The work will be done so easily. Isn’t it quite impressive?

This feature can be achieved through Slices.

Slices are the new concept from which you can embed your application content in other surface like Google Search app. Slices are part of Android P which is a great approach to its user interface. Slices can help user to complete work faster by providing app content to outside surface.

Slices are backward compatible that means it is compatible with Android Kit-Kat and further android version. So it concludes that slices are available for 95% of android devices. Isn’t it a great news?

What actually Slices are?

Slices demonstrate a piece of your app content. Slices are

  1. Templeted
    This means that slices provide rich layout and content system to express your app content in many ways.
    Using templates, you can add image, text, video to slice and make it more visible to user.
  2. Interactive
    Slices does not contain only static data. It contains bundle of variety of components.
    It provides real-time data, deep-links, inline actions, toggle button, sliders and scrolling content.
  3. Updatable
    We can iterate slices frequently by adding more presentable surfaces to expand your app reach.
    You can also add more templates and controls to make slices more engaging and more powerful for users.

Architecture Overview

Slice Provider extend a Content Provider. Slices are based on top of Content URI’s, which means you have wide variety of Slices hosted from your app.

When app wants to show your slice, your app gets a callback to onBindSlice() and you’ll get URI and you’ll get to decide what content you want to connect to that URI and return in that slice.

But, in interactive slice you have to update data in slice and these can be done through notifyChange() method. You send a standard content provider to notify change on your slice URI and whoever who present your slice will get to know that it’s time to update and they’ll give you callback and you can return updated data in response.

Where Slice is going to launch?

Slices are going to launch in search this year, where we are going to use slices for enhance app predictions as the user is searching.

There are two main use cases in terms of how slices will appear

  1. Application Name
  2. General tems

As the user searches with your application name, he will be able to view your application slice to jump over your application.

The other one is general searches like app deep-link or specific features.

Let’s make our first Slice

Before starting to work on slice you have to add necessary slice library. You’ll need to add slice-core and slice-builders to your app’s gradle file.

To create a slice, you have to make a one class which extend SliceProvider. Each slice contain uri and provider which bind slice with uri.

Slice Provider should be defined in your app manifest file which is responsible to find your slice by other surfaces.

This will handle all required permission internally so that you don’t have to define any permission separately.

Let’s implement class which extends SliceProvider

Now it’s time to build our first Slice
Slice is created with the help of ListBuilder class. You just have to create row which is displayed in slice.You can set title to the slice.

To run the application which includes slice, you have to install Slice Viewer. To make slice interactive you have to set action to it so that user can navigate to particular section of your application.

To run application you have to edit some configuration, to do so you have follow following steps.

  1. In your project, select Run > Edit Configurations
  2. In top-left corner, click + button and select Android App
  3. Enter slice in the name field
  4. Select your app module in the Module dropdown
  5. Under Launch Options, select URL from the Launch dropdown
  6. Enter slice- in the URL field
  7. Example: slice-content://com.example.your.sliceuri/path
  8. Click OK.

You can also refer these steps from here.

This is how you can create and run slice application in easy few steps. You can also add various component to your slice to represent it more attractive.

For example you can add temperature increase and decrease icon to your slice like this.

Conclusion

“A slice is designed to solve a problem: I’m a user and want to get something quickly done on my device,”

mm

Goral Ribadiya Android Developer

I am Android Developer at Yudiz Solution Pvt. Ltd. - a leading Mobile App Development Company. I am curious about learning new things in android and looking forward to explore new idea in field of android.

Comments are closed.

Top