HOW-TO Android 7.1: App Shortcuts

Paolo Rotolo
Glucosio Project
Published in
3 min readDec 9, 2016

--

After a few months of developer preview, this week Google officially released Android 7.1. Factory images are in fact already available for the most recent Nexus phones and, of course, Pixels.

One of the key features of Android 7.1 are App Shortcuts, a new way of interacting with apps directly from the home screen. If you tap for about a second on an icon on your home screen (even from your drawer) a menu will appear containing some shortcuts that will directly launch some specific features of the app.

Let’s say you want to add a new event to your calendar. Well, you don’t need to open the app, press on the add button and select event. You can just long tap on the icon and select New event: Android will directly open the “add event” view just for you :)

But wait, there’s more!

You can even drag the shortcuts on your home screen to always show them as icons.

Notice how we created a new icon from Gmail’s Compose shortcut. Also, a new smaller icon appears on the item, indicating that the item is actually only an action of the main app.

Cool, how I add them to my app?

Here comes the good news: you don’t need to write Java code. At all.

For static shortcuts, all you need to do is open your AndroidManifest.xml, find the activity whose intent filters are set to the android.intent.action.MAIN and the android.intent.category.LAUNCHERaction (usually the activity that fires when the user opens your app) and add a new<meta-data> element inside.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<application ... >
<activity android:name="Main">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="android.app.shortcuts"
android:resource="@xml/shortcuts" />

</activity>
</application>
</manifest>

And yes, we’re referencing a new file @xml/shortcuts. Create it!

Remember the app we’re developing to help diabetes people called Glucosio? Let’s take it as example. We want to add some shortcuts that allow the user to add new Glucose, Weight, A1C and Body Pressure reading directly from the home screen.

This is our shortcuts.xml file:

The syntax is simple, you need to open it with the <shortcuts> tag and then add a <shortcut> item for each shortcut you want to display. Each shortcut item has an icon, a label an enabled propriety and the intent you want to fire when it’s pressed.

You’re good to go, shortcuts are ready to be used!

You may also want to use dynamic shortcuts, with context-sensitive actions.

Only in THIS case, you need to use a little bit of code in Java (yes, I lied).

In this case, use the ShortcutManager API, you can find more about it on the Android Developers website.

To recap: STATIC shortcuts don’t require Java code while DYNAMIC ones do.

This is the result of our Shortcuts in the Glucosio app:

Happy coding! :)

--

--

Paolo Rotolo
Glucosio Project

Android Dev @ Blinkist. Lead @GDG Bari. Pursuing Master in Computer Engineering at PoliBa. Big #OpenSource supporter and #Kotlin fan.