App Shortcuts: Getting Started

Learn how to implement App Shortcuts in your Android app to make it more engaging for your users. By Andrej Vukelic.

Leave a rating/review
Download materials
Save for later
Share

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.
Note: This tutorial assumes you know the basics of Android development with Kotlin. If you’re new to Kotlin, check out our Kotlin introduction tutorial. If you’re completely new to Android development, read through our Beginning Android Development tutorials to familiarize yourself with the basics.

Getting Started

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.

Starting activity in QuickNotes app

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.

Actions related to the note

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.

Limitations

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.

Note: If you want to learn more about pushing dynamic shortcuts to Google Assistant follow this Google Codelabs.

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 ShortcutManagerCompat class.

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:

  1. Define shortcut properties like ID, enabled state, icon, long and short labels.
  2. Define screens that opens after a user selects the shortcut.
  3. Define category in which the note belongs./li>
  4. 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.

Note: Capability binding relates to the Built-in intents which gives a user the option to access the feature defined as a shortcut action through the Google Assistant. You can see how to achieve that here.

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!

Instructions how to create a note with Static shortcut

Okay, that was easy. Time to move on to the logic of creating dynamic shortcuts.