< Back to Blog

Adding Static Shortcuts to an existing Android App

2017-07-13

A quick article on how to implement Android Shortcuts

Google Photos App Shortcuts

This article was first posted on Medium.com. Click here to check out the original!

Static shortcuts were introduced in Android 7.1 (API Level 25) and make it super easy to give users quick access to your app.

I’m a big fan of Airbnb’s open source animation library Lottie. Animations have been notoriously difficult to create on Android and this library takes away all of that pain. To showcase this library, Airbnb have released a sample app on the play store. I use this frequently to view animations and play around with the awesome animated typography.

So shiny!

I decided to add static shortcuts to this sample app so I can access all of its features even quicker. The sample app consists of a main list activity with 4 options.

Tapping these options will either launch a fragment or activity. My goal was to bypass this initial screen with the shortcuts and launch each feature directly from the user’s home screen.

First I need to get some icons for my shortcuts!

Icons

Before diving in and creating your shortcuts it’s worth giving the App Shortcuts Design Guidelines a read through. This covers the specifics of creating a shortcut. Initially I used the fantastic Android Asset Studio by Roman Nurik as he has already developed a shortcuts icon generator. However after some PR feedback I created my own vector drawables (Android asset studio only generates png). For quick reference the outer circle is 48dp by 48dp and the inner icon should be 24dp by 24dp. Even a sketch novice like myself managed to create some decent-looking icons!

Harnessing my inner sketch-fu

Code

Following the steps in the dev docs you must first define your shortcuts.xml file in your manifest.

<meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcuts" />

Then create your shortcuts file and add shortcuts accordingly.


<shortcut
 android:enabled="true"
 android:icon=
 "@drawable
 /ic_shortcut_animation_viewer"
 android:shortcutDisabledMessage=
 "@string/shortcut_disabled"
 android:shortcutId="animationViewer"
 android:shortcutLongLabel=
 "@string/shortcut_animation_viewer"
 android:shortcutShortLabel=
 "@string/
 shortcut_animation_viewer_short"
 tools:targetApi="n_mr1"
 >
 <intent
   android:action=
   "com.airbnb.lottie
   .samples.shortcut.VIEWER"
   android:targetClass=
   "com.airbnb.lottie
   .samples.MainActivity"
   android:targetPackage=
   "com.airbnb.lottie"
   />
 </shortcut>

It’s pretty much as simple as that! After defining which icon and text you wish to show it’s mostly a matter of specifying your target action and class.

For the case of Lottie I wanted to launch the main list activity for each shortcut then open the corresponding screen, this way the back button would navigate to the list as expected and not exit the app. To achieve this I set a specific action for each shortcut and then retrieved this action using getIntent().getAction().


@Override public void onCreate(@Nullable Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  handleShortcut(getActivity()
  .getIntent()
  .getAction());
}

.....

\_/\*\*

- Starts the relevant activity/fragment based on which shortcut has been pressed
- @param intentAction specific shortcut action
  \*/
  _private void handleShortcut(String intentAction) {
  switch (intentAction) {
  case \_SHORTCUT_VIEWER_:
  onViewerClicked();
  break;
  case _SHORTCUT_TYPOGRAPHY_:
  onTypographyClicked();
  break;
  case _SHORTCUT_TUTORIAL_:
  onAppIntroPagerClicked();
  break;
  case _SHORTCUT_FULLSCREEN_:
  onFullScreenClicked();
  break;
  }`} 

If you need more control over your shortcuts you can use dynamic shortcuts. The ShortcutManager API can create, update and delete shortcuts on the fly.

Static shortcuts for Lottie

Here’s my pull request for Lottie Android if you want to go through the code in its entirety.

Thanks for reading!