Icing on the Slice: Providing more value to users with range actions

Big Nerd Ranch
Big Nerd Ranch
Published in
3 min readOct 23, 2018

by: Brian Gardner

Slice actions help users perform the actions they want without having to search through your app.

While basic slices have their place, you will often want to provide users with additional information or controls. Range actions make your Slices more interactive by allowing users to control any field that accepts a discrete range of options, such as volume or brightness.

Slices provide the ability for your app to share information and controls with other apps. In my previous post, Share a Slice of Your App, I covered the basics of Slices on Android. Introduced in Android Pie (API 28), they are packaged as a library and work all the way back to Android KitKat (API 19). If you haven’t already, give my introduction to Slices a read through to learn about the main concepts in Slices, how to create a simple one, and how to view them on your devices. Before continuing with this post, make sure you have a SliceProvider in place to build off of for the interactive slices.

In my previous post, I covered adding a toggle action to your Slice to make it more interactive. In this post, I’ll show how to add a range action. Ranges allow users to control any field that accepts a discrete range of options, such as volume or brightness. This type of control also requires you to create a component to handle the data in your app. Read on to find out more!

Add range action

Similar to the toggle action, you need to provide a Service or BroadcastReceiver to handle the data coming from the Slice. For this example, I'll use a BroadcastReceiver. Don't forget to register your receiver in your AndroidManifest.xml so the system can start it properly.

Again, I’ll fill out the actual implementation once I complete my Slice. This will allow me to create a valid PendingIntent for my Slice action.

Next, I’ll create a different path for my Slice called /range.

One important thing to know about the range action is that you need to specify a primary Slice action. If you don’t, the app will crash with an IllegalStateException. So first, I'll create my primary action for my range Slice:

With that in place, I can move on to creating my Slice. The inputRange is used to create the range action. A PendingIntent is provided for the component responsible for handling the updates. A max and current value are also provided to configure the current state of the range.

With my slice implementation in place, I can fill out my BroadcastReceiver. Like the toggle implementation, the Intent passed to the receiver has extra data about the current value of the range. This method will be called many times as the user moves the slider so don't take too long in your receiver or you risk the system killing your receiver before it finishes.

With my receiver in place, I can run my Slice and see the range value output to LogCat as I move the slider.

Fin

Range actions provide a great user interface for users to control any functionality that needs a value from a discrete range. You can either use a Service or a BroadcastReceiver to handle the user's input. Just configure a PendingIntent for your component and the system will start it when the data changes.

I hope you enjoyed these posts on interactive Slices. Watch out for more posts on Slices in the future. If you have any questions please feel free to comment below. Thanks for reading!

Originally published at bignerdranch.com.

--

--

Big Nerd Ranch
Big Nerd Ranch

Designing and building exceptional apps. We teach others to do the same through our bootcamps and corporate training.