Implementing Android App Shortcuts

Implementing Android App Shortcuts

With the release of Android Nougat 7.1(API level 25) a new features is added i.e. 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. Users can also pin these shortcuts to the home screen for even quicker access to your app’s primary actions.

You can publish the following types of shortcuts for your app:

  • Static shortcuts are defined in a resource file that is packaged into an APK. Therefore, you must wait until you update your entire app to change the details of these static shortcuts.
  • Dynamic shortcuts are published at runtime using the ShortcutManager API. During runtime, your app can publish, update, and remove its dynamic shortcuts.
  • Pinned shortcuts (Android 8.0 (API level 26)) are published at runtime and also use the ShortcutManager API. During runtime, your app can attempt to pin the shortcut, at which time the user receives a confirmation dialog asking their permission to pin the shortcut. The pinned shortcut appears in supported launchers only if the user accepts the pinning request.

Note: Users can also create pinned shortcuts themselves by copying your app’s static and dynamic shortcuts onto the launcher.

Google Map Shortcuts

Google Map Shortcuts

So today we are going to learn how to implement above mentioned shortcuts in our app and improve user interaction with our app.

VIDEO DEMO

Static Shortcuts

Static shortcuts provides links to some actions within your app and these actions should remain consistent over the lifetime of your app’s current version. Some examples for whom you can add static shortcuts in your app:

  1. Viewing Message Inbox (Send/Received).
  2. Setting an Alarm.
  3. Open call logs (Missed/Received/Dialled).

To create Static shortcuts follow the below steps:

1. Add the below meta-data to your Launcher activity in AndroidManifest.xml.

After adding your AndroidManifest will look like this:

2. After adding meta-data you will be getting error for @xml/shortcuts. So for that create a new resource file:  res/xml/shortcuts.xml.

Create new Android Resource Directory

Create new Android Resource Directory

 

Select XML from Dropdown

Select XML from Dropdown

 

In this new resource file, add a <shortcuts> root element, which contains a list of <shortcut> elements. Each <shortcut> element, in turn, contains information about a static shortcut, including its icon, its description labels, and the intents that it launches within the app:

I have added two intent under one shortcut one after other to maintain navigation. The last intent declared in <shortcut> tag will open when clicked on shortcut.

Static Shortcut

Static Shortcut

Check this link for App Shortcuts design guidelines.

Dynamic Shortcuts

Dynamic shortcuts provides links to specific, context-sensitive actions within your app. These actions can change between uses of your app, and they can change even while your app is running.

Some examples for whom you can add static shortcuts in your app:

  1. Calling a specific person.
  2. Navigating to specific location.
  3. Sending message to specific person, etc.

To implement Dynamic shortcuts we have to use  ShortcutManager API. These API provide following methods:

Static and Dynamic Shortcuts

Static and Dynamic Shortcuts

 

Updated Dynamic Shortcut

Updated Dynamic Shortcut

 

  • Disable: Disable a set of dynamic pinned shortcuts or single dynamic pinned shortcut using disableShortcuts(). To disable shortcuts we have to pass the ID of the shortcut.
Disabled Dynamic Shortcut

Disabled Dynamic Shortcut

 

Click event on Disabled Dynamic Shortcut

Click event on Disabled Dynamic Shortcut

 

After disabling Dynamic Shortcut

After disabling Dynamic Shortcut (Disabled shortcut not showing in Shortcut List)

  • Enable: Enable a set of dynamic pinned shortcuts or single dynamic pinned shortcut using enableShortcuts(). To enable shortcuts we have to pass the ID of the shortcut.
Enabled Dynamic Shortcut

Enabled Dynamic Shortcut

Pinned Shortcuts

On Android 8.0 (API level 26) and higher, you can create pinned shortcuts. Unlike static and dynamic shortcuts, pinned shortcuts appear in supported launchers as separate icons.

To pin a shortcut to a supported launcher using your app, follow the below steps:

  1. Use isRequestPinShortcutSupported() to verify that the device’s default launcher supports in-app pinning of shortcuts or not.
  2. Create a ShortcutInfo object in one of two ways, depending on whether the shortcut already exists:=> If the shortcut already exists, create a ShortcutInfo object that contains only the existing shortcut’s ID. The system finds and pins all other information related to the shortcut automatically.
    => If you’re pinning a new shortcut, create a ShortcutInfo object that contains an ID, an intent, and a short label for the new shortcut.
  3. Attempt to pin the shortcut to the device’s launcher by calling requestPinShortcut(). During this process, you can pass in a PendingIntent object, which notifies your app only when the shortcut is pinned successfully.

Note: If the user doesn’t allow the shortcut to be pinned to the launcher, your app doesn’t receive a callback.

You can also remove, update pinned shortcuts.

Prompt to add Pinned Shortcut to Home Screen

Prompt to add Pinned Shortcut to Home Screen

 

Pinned Shortcut added to Home Screen

Pinned Shortcut added to Home Screen

 

Things to know about App Shortcuts

  1. Publish only four shortcuts including static and dynamic.
  2. Short description length of a shortcut should be of 10 characters and Long description should be of 25 characters.

Full Source

1. Open string.xml and add the below strings to it. We will use this strings in app.

2. Open MainActivity.java and add the below code to it. This class contains all three types of Shortcuts.

3. Open activity_main.xml and add the below layout to it. This layout contains some button to add, update and remove shortcuts.

4. Open AndroidManifest.xml and add the below code to it.

5. Finally all done, now you can also implement App Shortcuts in your app.

Thanks. :)

Download Source Code

 

About the author

I am an Android developer for whom Android is the one and only love. I work as a freelancer which gets me and Android closer. I always love to share my learnings and love to learn from others knowledge too.

Leave a Comment

Comment (required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Name (required)
Email (required)