App Shortcuts: Getting Started
Many of the most commonly used apps consume App Shortcuts to provide users with easy access to its most important features. For example, Instagram provides shortcuts to Camera, New Post, View Activity, and Direct messages, while WhatsApp provides Camera and four conversations.
All these apps consume the
ShortcutManager API for easy shortcut creations.
In this tutorial, you’ll learn how to manage three different shortcut types:
- Static shortcuts through the xml file.
- Dynamic shortcuts through the code.
- Pinned shortcuts through the code.
Download the starter project by clicking on the Download Materials button at the top or bottom of the tutorial. Then, open the starter project in Android Studio 3.5 or later.
Take a look at the code. You’ll find the starter project with activities, a layout, a shortcut wrapper, and many TODOs.
Build and run the starter project. You’ll see an empty screen with a
FabButton that launches a new
Activity where you can create a new Note.
Try using the app. Tap the
FabButton and create a new note. A new note will be visible in the list. Long press the note, and
BottomSheetDialogFragment will show with two actions.
Now you’ll see what types of shortcuts are present and see what type you should use in which case.
Introducing App Shortcuts
Shortcut performs specific action in the specific app, triggered from the:
- Supported launcher.
- Assistant – e.g. Google Assistant.
Android app shortcuts could make your app more efficient if they’re used correctly.
A developer has a duty to take care of which features will provide in the app shortcut list, but a user needs to discover them and find them helpful.
Even for people like us, Android developers, it’s hard to remember how many powerful tools Android provides for the easier use of the device. One of these is app shortcuts functionality.
Although app shortcuts are already easy to access by long-pressing the icon on the home screen or app list, a user can pin all the shortcuts to the home screen for even easier access.
Developers should always consider supporting shortcuts, even if it’s hard for users to find them.
The next section will give you an overview of the app you’ll create in this article.
Types of Shortcuts
How you deliver content with shortcuts depends on your use case and whether the shortcut’s context is app-driven or user-driven. No matter if the shortcut’s context changes, both static and dynamic shortcuts are driven by your app.
In cases where a user chooses how they want your app to deliver content to them, such as with a pinned shortcut, a user defines the context.
The following scenarios show a few use cases for each shortcut type:
- Static shortcuts: utilized for the content with the consistent structure through the lifetime of a user’s interaction. Instagram is using it for quick access to the Posts, Camera, or Direct messages.
- Dynamic shortcuts: used for the actions that are context-sensitive. They’re made for the actions users perform in an app. For example, if you’re building a game that allows the user to start from their current level, you need to update the shortcut often.
- Pinned shortcuts: used for specific, user-driven actions. The best example is pinning a specific website from your favorite browser. This is beneficial because it allows the user to perform a custom action. For e.g. quick navigation to the specific website
Although we have different types of shortcuts that cover all use cases, there are some limitations that we need to be aware of.
Even though users can create as many dynamic shortcuts as they want, most supported launchers display up to four shortcuts at a time.If the app supports dynamic shortcuts, choose static shortcuts carefully, so there’s still some space for a user to create dynamic ones.
Yet, for the dynamic shortcuts used with Google Assistant, use Google Shortcuts Integration Library to avoid the limitation.
If you choose not to use the Google Shortcuts Integration Library, your app will support a limited number of shortcuts at a time.
Sometimes you’ll have to decide whether you want more static shortcuts or let the user create dynamic ones.
To determine how many shortcuts the launcher supports, use
getMaxShortcutCountPerActivity() method provided in
But, if your app supports pinned shortcuts, you’re safe from these limitations. Launchers don’t have a maximum number of pinned shortcuts.
It’s time to jump in on the actions used to manage shortcuts!
Managing App Shortcuts
As you already know, there are three different types of shortcuts, and the user manages dynamic ones.
The user is able to do several actions with the shortcuts:
- Push: creates a new dynamic shortcut.
- Update: updates dynamic shortcuts that already exist.
- Remove: removes dynamic shortcuts from the list.
- Remove All: removes all dynamic shortcuts.
- Reordering: adds/updates dynamic shortcuts with the rank property.
Reordering is an action that happens as a side effect of Push or Update actions. Access these action on the
ShortcutManagerCompat API, and they’re super easy to use.
Working With Shortcuts
In this section, you’ll find all the actions available for managing the app shortcuts and how to implement them.
Creating Static Shortcuts
Now, you’re going to start by creating static shortcuts with predefined and unchangeable actions.
Firstly, in the res package, under xml subfolder, find the shortcuts.xml file and open it.
In this file, you’ll find
TODO 1: which you’ll replace with the following code:
<!-- 1 --> <shortcut android:shortcutId="new_note" android:enabled="true" android:icon="@drawable/ic_new_note_shortcut" android:shortcutLongLabel="@string/new_note_long_label" android:shortcutShortLabel="@string/new_note_short_label"> <!-- 2 --> <intent android:action="android.intent.action.VIEW" android:targetClass="com.yourcompany.quicknotes.screen.notes.NotesActivity" android:targetPackage="com.yourcompany.quicknotes" /> <intent android:action="android.intent.action.VIEW" android:targetClass="com.yourcompany.quicknotes.screen.newnote.NoteActivity" android:targetPackage="com.yourcompany.quicknotes" /> <!-- 3 --> <categories android:name="com.yourcompany.quicknotes" /> <!-- 4 --> <capability-binding android:key="actions.intent.CREATE_NOTE" /> </shortcut>
This is what you’re code does:
- Define shortcut properties like ID, enabled state, icon, long and short labels.
- Define screens that opens after a user selects the shortcut.
- Define category in which the note belongs./li>
- Define the capability which links the shortcut with the built-in intents.
It’s essential to define the shortcut action properly. When you create a shortcut, allow users to open the screen flow that defines the feature they need.
Your next step is to give an app instructions to create static shortcuts. Go to the manifests folder and open
AndroidManifest.xml file and replace
TODO 2: with the following code snippet:
<meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcuts" />
Your shortcut is ready to use. Build and run the app, go to launcher and long press on the app icon. Press the shortcut, and you’re ready to create a new note!
Okay, that was easy. Time to move on to the logic of creating dynamic shortcuts.