Navigating with App Shortcuts
Not long ago, iOS users were introduced to a new feature called 3D Touch. What this feature does is provide a limited list of actions that you can do without entering an app. Of course, the people from Google were ready to respond to that. Boom!!! the App Shortcuts feature was included in Android 7.1.1
App Shortcuts
App Shortcuts are designed to do common actions in the application from the launcher screen. We can reach certain screens by just long-pressing the app icon and clicking the respective shortcut.
Types of AppShortcuts
- Static Shortcuts
- Dynamic Shortcuts
- Pinned Shortcuts
Static Shortcut
These shortcuts are predefined. They come together with your .apk. Their number and actions stay the same until you publish a new version of your app.
Dynamic Shortcut
These shortcuts are constructed and included in the list with shortcuts on the fly. They can be created based on some usage statistics and can change over time. There is no need to publish a new version of your app, to update your dynamic shortcuts.
Pinned Shortcuts
These are used for specific, user-driven actions. For example, a user might want to pin a specific website to the launcher. This is beneficial because it allows the user to perform a custom action, like navigating to the website in one step, more quickly than using a default instance of a browser.
Some famous apps using the App Shortcuts feature
- Google Maps
- Google Play Music
- Google Photos
- Google Drive
- Chrome
- YouTube
- Evernote
Why do we need App Shortcuts?
App Shortcuts allow the user to access primary actions in your app straight from the launcher, taking the user deep into your application, by long-pressing on your app icon.
Best Practices
Don’t overdo it, please…
- Follow the design guidelines
- Publish only four distinct shortcuts
- Limit shortcut description length -> 10 short description, 25 long description
- If using system icons, their size should be 24 x 24 (DP).
- Use SVG icons for automatic scaling.
Limitation
According to the official Android documentation, the current API supports up to five different shortcuts at any given time. BUT it is highly recommended to use a maximum of four of them.
Another limitation is the launcher of the current device. Not all launchers support App Shortcuts yet. That is, before releasing this new feature we must test it on as many different launchers as possible.
For the web Nation
App Shortcuts works perfectly with PWA (Progressive Web App) and supports:
- Chrome 84 for Android
- Chrome 84 and Edge 84 for Windows
Check this slide to know more
How can we implement it?
Before we start there are a couple of attributes that I will like to go over, so we understand what we are doing.
We will create three (3) static shortcuts in the app, so for each shortcut, we must provide:
- shortcutId: The ID of the shortcut.
- enabled: The current state of the shortcut. It can be either true or false. If the shortcut is disabled it won’t appear in the list with shortcuts.
- shortcutShortLabel / shortcutLongLabel: The labels which contain the name of the shortcut. Their length is limited. Usually, the short one is used when the shortcut is pinned to the home screen. The long label is used for the app shortcut menu.
- shortcutDisabledMessage: The message, which is shown to the user when a disabled pinned shortcut was selected.
Codes Snippet goes here….
Static Shortcuts
Step 1
Add the following code snippet in your AndroidManifest.xml file, if you don’t add it, your shortcuts will not appear, we have to add some <meta-data>
to the launcher Activity. In practice, any Activity with the intent filter is set to action. The android: resource is where you specify where the shortcuts files are located in your resource folder.
Step 2
Navigate to shortcuts.xml where the shortcuts are defined. I had to set the tools:targetApi=”25" because Android Studio just dey shout anyhow (meaning Android Studio keeps warning me). It’s a good practice regardless because this feature is only available on API level 25 and above. once you are done run your app on your emulator or device to see the shortcuts.
Dynamic Shortcuts
Dynamic shortcuts are the second type of shortcuts, and to interact with them (create/destroy/update) you’ll need to use the ShortcutManager. You can get a hold of a ShortcutManager using
ShortcutManager
uses system service so this has to be in an activity and remember this only works on Android 7.1 so it's good to add the annotation @TargetApi(25)
to this code to avoid compile errors and add version check before calling these methods.
To create a new shortcut, we have to use ShortcutInfo
. Here shorcut2
is the id given to the shortcut.
setRank()
the method is used to order the dynamic shortcuts in the shortcuts pane. it is used to position the shortcut in the list, 0(zero) being the lowest positioned one.
Now that we have our shortcut ready, we need to set it as a Dynamic Shortcut.
dynamicShortcuts
method takes an Array. Since we have two shortcuts, I have created a listOf()
.
manager.removeAllDynamicShortcuts()
will remove all the dynamic shortcuts.
Run your app and long-press the app icon in the launcher. You should see something like this:
Code is available here on Github
Thank you for reading this article. Be sure to clap and recommend this article if you found it helpful and insightful. It means a lot to me
Chat me up on Twitter or Linkedin
References
https://developer.android.com/guide/topics/ui/shortcuts/creating-shortcuts