What is an App Shortcut?
App Shortcuts uncover common tasks or activities of your application to the launcher screen. Users can use the App Shortcuts feature by long-press on the app icon.
- App Shortcuts are great for making common actions of your app visible and bring back users into the flow in your application.
- App Shortcuts can be static or dynamic.
- Static remain same after you define them, you have to redeploy app if you want to make any changes in it.
- Dynamic one can be changed any time or on the fly.
- You can create/manage the stack of activities once you open one through a shortcut. Here you can set the flow of all the activities using intents.
- You can reorder the shortcuts in their respective. Static shortcuts will come always at the bottom as they’re added first (there’s no
rankproperty to be defined on them)
- The labels are char sequence. So you can play with the span in order to change anything in the text of the labels.
App shortcuts can be of two types-
Static Shortcuts– They are defined statically in a resource file; you can’t change without redeploying the build.
Dynamic Shortcuts– The one that can be defined at runtime or on the fly; you can change without redeploying the build.
Note: App must have a minimum SDK version set to API 25.
Let’s start with creating Static Shortcut first.
Open your AndroidManifest.xml and add the following meta-data tag to your parent or main activity which you want to open as soon as your app starts playing the role.
You can add multiple shortcuts in the root tag <shortcuts>. Let’s understand the properties one by one.
enabled: It indicates that the shortcut is enabled or not. If you want to disable this shortcut, just set the parameter value as false.
icon: It will set the icon to your shortcut menu on the left side.
shortcutDisabledMessage: This will show the message if someone will click on the disabled shortcut. However if you have set the “enabled” feature as false, the option will not be shown when you will do long-press but the user can pin a shortcut to the launcher. So pinned shortcut will be disabled at that time and click on it will show a toast message to the user.
shortcutLongLabel: The longer text that will be shown as a label but it will only be shown if there is enough room.
shortcutShortLabel: A short description of the shortcut. This field is mandatory.
intent: The intent will be invoked when the user will click the shortcut icon.
Multiple intents in shortcuts tags will maintain the stack of the activity as you can see in the above example. When you will press the shortcut icon, then “MainActivity” followed by “MyStaticShortcutActivity” will be opened, and when you press the back button it will take you to the “MainActivity”.
The dynamic shortcuts can be modified on the fly without the need of redeploying your app. They are created in java code.
I will show you how you can add dynamic shortcuts. We will use the ShortcutManager and ShortcutBuilder.Info in order to create dynamic shortcuts.
Here we have built the shortcut info using
shortcutManager. We can set the properties for the shortcut using it. The properties are the same except id and set rank. So I will explain these two only in case of dynamic shortcuts and other properties have same behavior as static shortcuts.
Here in the above example, we have defined the id shortcut_dynamic as the second parameter of ShortcutInfo.Builder and setRank() are used to set the order of the shortcut which is not present in the case of static shortcuts. Here the first static shortcut will appear followed by rank 0 and then rank 1 shortcut.
As you can see in above image that static shortcut always takes place at the bottom of the list. You cannot change the rank of static shortcuts. They will always be shown in the order they’re defined in the file
If we see the method
ShortcutInfo.Builder we can see that it takes
CharSequence as a parameter. Which means that we can play around with it. We can change its color on the fly. We can create a
SpannableStringBuilder and set to it a
ForegroundColorSpan with the color, we want to apply and then pass the
spannableStringBuilder as a
shortLabel (as the
SpannableStringBuilder is a