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
You are currently viewing page 2 of 2 of this article. Click here to view the first page.

Creating Dynamic Shortcuts

Dynamic shortcuts are a little bit tricky but still very easy to implement.

As before mentioned, you’ll need to use the ShortcutManager API. It is part of the Jetpack Libraries that lets you manage dynamic shortcuts within your app. It reduces boilerplate code and, most importantly, ensures that shortcuts behave the same across Android versions.

To add the functionality which will create a new dynamic shortcut, open the ShortcutManagerWrapper.kt file and replace TODO 3: with these lines of code:

return ShortcutInfoCompat.Builder(context, note.id)
    .setShortLabel(note.title)
    .setLongLabel("See ${note.title}")
    .setIcon(IconCompat.createWithResource(context, R.drawable.ic_note))
    .setIntents(
        arrayOf(
            NotesActivity.createIntent(context).apply { action = Intent.ACTION_VIEW },
            NoteActivity.createIntent(context, note.id).apply { action = Intent.ACTION_VIEW }
        )
    )
    .build()
Note: It’s likely that you have to make some imports for the code to work.

You can see that a dynamic shortcut acts the same as the static one. The difference is that now you have logic for making the shortcut more context-sensitive.

To fully enable creating dynamic shortcuts, replace TODO 4: with these two lines of code:

val shortcut = createShortcutInfo(note)
ShortcutManagerCompat.pushDynamicShortcut(context, shortcut)

The method pushDynamicShortcut(...) will do:

  1. Check limitation of the maximum number of shortcuts.
  2. Prepare shortcuts depending on the Android Verson.
  3. Remove shortcut with the lowest rank.
  4. Add a new one to the top of the list.

Awesome, you’re now ready to create your very first dynamic shortcut. Build and run the app, long-press the created note, and then long-press the app icon to show the shortcuts.

Instructions how to create a Dynamic Shortcut

The next step is enabling updates for the dynamic shortcut you’ve added.

Updating Dynamic Shortcuts

Updating a dynamic shortcut is the same as creating a new one.

In the QuickNotes app, your shortcut will update when you open an existing note and modify the content by pressing the update action in the top right corner.

Next task for you is to implement it so you can update the shortcut when needed.

In the ShortcutManagerWrapper.kt file, find TODO 5: and replace it with the following:

addNoteShortcut(note)

That’s it! Easy, right? :)

Try to update your existing note. Open it from the shortcut you’ve added, update it and confirm that the app has updated the note. Now go and check the shortcut you previously created.

You will create another note and the shortcut for it. Close the app, open the shortcut list, and verify that the bottom-most shortcut is the one you created first.

Creating a second shortcut

Open the first note, change the title, and press update. If you check the shortcuts, you’ll see that you’ve changed the order. The one that you’ve edited is now at the top. Updating the shortcut also changes the shortcut rank.

Update first shortcut to make a change in order

Good job, you’re next step is to delete the shortcut.

Deleting a Dynamic Shortcut

To delete a dynamic shortcut, the first thing you’ll need to know is if the shortcut already exists.

Find TODO 6: and replace it with the following block of code:

override fun isShortcutCreated(noteId: String): Boolean {
    return ShortcutManagerCompat.getDynamicShortcuts(context)
        .find { it.id == noteId } != null
}

Check if the shortcut with the requested note exists in the list of dynamic shortcuts.

Since you don’t want your user to have the option to access a note once it gets deleted from the database or service, you’ll need to delete the shortcut too.

In the ShortcutManagerWrapper.kt find TODO 7: and replace it with these lines of code:

ShortcutManagerCompat.removeDynamicShortcuts(context, listOf(noteId))

Great, a user is now able to create, update and delete a dynamic shortcut, go ahead and try it. Build and run the app, and follow the next steps in order to check that everything works:

  1. Create a new note.
  2. Create a dynamic link.
  3. Verify that it’s created by long pressing on the app icon.
  4. Update the note title.
  5. Verify that it’s updated in the shortcut list.
  6. Delete the note.
  7. Verify that the shortcut has been deleted from the shortcut list.

Congratulations! You’ve implemented the dynamic shortcut feature! Now, you’re moving to the last type of shortcut.

Creating Pinned Shortcuts

There is only one type of shortcut left to implement. Creating a pinned shortcut is a little bit different than the others.

Find TODO 8: and replace it with this code snippet:

// 1
if (ShortcutManagerCompat.isRequestPinShortcutSupported(context)) {
    // 2
    val pinShortcutInfo = ShortcutInfoCompat.Builder(context, note.id)
        .setShortLabel(note.title)
        .setIcon(IconCompat.createWithResource(context, R.drawable.ic_note))
        .setIntents(
            arrayOf(
                NotesActivity.createIntent(context).apply { action = Intent.ACTION_VIEW },
                NoteActivity.createIntent(context, note.id).apply { action = Intent.ACTION_VIEW }
            )
        )
        .build()
    // 3
    ShortcutManagerCompat.requestPinShortcut(context, pinShortcutInfo, null)
}

It’s easier to understand if it’s broken down into smaller pieces:

  1. Verify that the default launcher supports pinned shortcuts.
  2. Create ShortcutInfo with the short label, icon and intent which leads to
    desired Activity.
  3. Request a pin shortcut which will trigger native dialog with the shortcut info on newer Android versions, or create a shortcut instantly on versions below API 26.

Build and run the app, long-press the note, and create your first Quick Notes pinned shortcut.

Go to the launcher and check if it’s there.

Instructions how to create pinned shortcut

You’re just one press away from the tasks you’ll need for that day, shopping cart, or whatever it is you want to note. :)

Congratulations! You’re ready to make your app more accessible for the user. Think about the most important features you want to provide and don’t forget about the limited number of shortcuts.

Where to Go From Here?

You can download the complete project by clicking Download Materials at the top or bottom of this tutorial.

In this article, you’ve learned how to create different types of app shortcuts and how to use them.

As mentioned earlier, you can make your app easier to access with Google Assistant. You can find here an awesome article on how to create Actions for Google Assistant.

We hope you enjoyed this tutorial. If you have any questions or comments, please join the forum discussion below!