Android App shortcuts – How and When to use them

App Shortcuts were introduced Android Nougat 7.1. They allow you to access primary actions of your app directly from the launcher icon.

You can also pin certain shortcuts to the launcher for faster access.

Consider you have the Uber app and you want to book a ride home. You would normally open up the app and then book a cab with your Home as the destination. Instead of doing all this, wouldn’t it be great if a there was a shortcut right in your launcher that does all this in once click? That’s exactly where App Shortcuts come in.

In this post, we will learn how to easily implement App Shortcuts.

App shortcuts give users quick, easy access to up to four of your app’s actions. Each action can also be added to the home screen.

Google messenger app shortuts

Image credits: Google

 

Design Guidelines

Google Design Guidelines

Google Design Guidelines

Before we start coding, it is essential to know the design guidelines for App Shortcuts:

  • The shortcut icons should have a total dimension of 48dp x 48dp
  • The Live Area (where your icon will reside) should be  44dp x 44dp
  • The Live area should have a solid background colour fill of Material Grey 100 (or #F5F5F5)
  • The icon should have dimensions of 24dp x 24dp at the centre for the Live Area
  • Shadows should NOT be included
  • The icon colour has a strong contrast with Material Grey 100

You can read more about the guidelines here.


There are 2 types of App Shortcuts:

1. Static App Shortcuts

These are generated using XML and can be updated only when the app is updated. Static App Shortcuts should be only be used for actions that are constant and core to the app.

For example, the Twitter app provides shortcuts to start a Search, make a new Tweet, send a new message, etc. These actions are essential to the app and are not affected by user behaviour. Hence it makes sense to implement them as Static shortcuts.

Twitter app shortcuts

Twitter app shortcuts

Adding a Static Shortcut

In AndroidManifest.xml, we have to add some <meta-data> to the launcher Activity. In practice, any Activity with the intent filter set to action android.intent.action.MAIN and category android.intent.category.LAUNCHER can be used for app shortcuts. In my <meta-data> tag, I have specified the name as android.app.shortcuts and resource as @xml/shortcuts.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.subhrajyoti.appshortcuts">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <meta-data
                android:name="android.app.shortcuts"
                android:resource="@xml/shortcuts" />
        </activity>
        <activity android:name=".StaticShortcutActivity"
            android:parentActivityName=".MainActivity"
            android:theme="@style/AppTheme"/>
    </application>

</manifest>

@xml/shortcuts is the file where we define our static shortcuts. In your res directory, create another resource directory called xml if there already isn’t one. Inside that directory, create an XML file called shortcuts.xml with the following contents.

    <shortcut
        android:enabled="true"
        android:icon="@drawable/ic_open"
        android:shortcutDisabledMessage="@string/static_shortcut_disabled_message"
        android:shortcutId="staticShortcut"
        android:shortcutLongLabel="@string/shortcut_long_label"
        android:shortcutShortLabel="@string/shortcut_short_label">
        <intent
            android:action="android.intent.action.MAIN"
            android:targetClass="com.subhrajyoti.appshortcuts.MainActivity"
            android:targetPackage="com.subhrajyoti.appshortcuts" />
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.subhrajyoti.appshortcuts.StaticShortcutActivity"
            android:targetPackage="com.subhrajyoti.appshortcuts"/>
    </shortcut>
</shortcuts>

Each shortcut that you define should be enclosed in a <shortcut> tag.

Attributes explained
  • android:enabled 
    Boolean to enable or disable the shortcut. If a shortcut is disabled, it will not show up in the list of shortcuts in the launcher.NOTE:
    If the disabled shortcut was previously pinned, it will be greyed out and clicking on it will display the message specified by the string in android:shortcutDisabledMessage
  • android:icon 
    Icon resource to display corresponding to the App Shortcut
  • android:shortcutId
     Unique identifier that allows Android to recognise your shortcut
  • android:shortcutShortLabel 
    Your App Shortcut’s text. It should be within 10 characters. Otherwise, it will be ellipsised.
  • android:shortcutLongLabel
    Label that is displayed when the shortcut is pinned to the launcher. It should have a max length of 25 characters.

Every <shortcut> needs to have at least one <intent> tag that specifies what action is to be taken when a shortcut is clicked.

You can have multiple <intent> tags in the same shortcut but Android will handle this by using the last <intent>. It places all the previous intents in the backstack.

That’s all you need to get static shortcuts running.

2. Dynamic App Shortcuts

Telegram app shortcuts

Telegram app shortcuts

Dynamic shortcuts can be generated and destroyed at runtime. They are used for actions customised to the user’s behaviour.

For example, the Telegram app provides shortcuts to your most frequented chats.

Adding a Dynamic Shortcut

In order to add, remove or update dynamic shortcuts, we need to use the ShortcutManager class.

ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);

Let’s create our Intent that will be used by our shortcut.

Intent intent = new Intent(this, DynamicShortcutActivity.class);
intent.setAction(Intent.ACTION_VIEW);

To create a new shortcut, we have to use ShortcutInfo.

ShortcutInfo dynamicShortcut = new ShortcutInfo.Builder(this, "dyanmicShortcut")
                .setShortLabel("Dynamic")
                .setLongLabel("Open dynamic shortcut")
                .setIcon(Icon.createWithResource(this, R.drawable.ic_dynamic))
                .setIntent(intent)
                .setRank(0)
                .build();

Here dyanmicShortcut is the id given to the shortcut.

The setRank() method is used to position the shortcut in the list, 0(zero) being the lowest positioned one.

Now that we have our shortcut ready, we need to set it as a Dynamic Shortcut.

shortcutManager.setDynamicShortcuts(Collections.singletonList(dynamicShortcut));

The setDynamicShortcuts() method takes an Array. Since we have only shortcut, I have a created a Singleton List.

Finally, add an <intent-filter> to the DynamicShortcutActivity in AndroidManifest.xml

<activity android:name=".DynamicShortcutActivity"
       android:parentActivityName=".MainActivity"
       android:theme="@style/AppTheme">
       <intent-filter>
           <action android:name="com.subhrajyoti.appshortcuts.OPEN_DYNAMIC_SHORTCUT" />
           <category android:name="android.intent.category.DEFAULT" />
       </intent-filter>
</activity>

That’s it, we’re done! Now our Dynamic Shortcut is ready to roll.

Run your app and long press the app icon in the launcher. You should see something like this:

App shortcuts demo

App shortcuts demo

Disabling a Shortcut

To disable a certain dynamic shortcut, we can use the disableShortcuts() method. It takes an array of shortcut ids as parameter

shortcutManager.disableShortcuts(Collections.singletonList(dynamicShortcut));

NOTE:
You can have a maximum of 5 App Shortcut. The number of shortcuts should be chosen carefully. Attempting to use more than 5 will result in a crash.

Not all launchers support app shortcuts. Make sure to test in on launchers that have App Shortcuts support (like Nova launcher).

When not to use App Shortcuts

App shortcuts should be used only for the most commonly used features of the app.

Let us consider WhatsApp as an example. The ideal shortcuts for it would be the ‘most frequent chats‘. This is where the user spends the most time on the app. Hence it makes sense to include them in the shortcuts.

But it should not be used for actions like creating a new group as it is not a core or frequently used feature. Using shortcuts for actions the user does not use that often beats the point of having shortcuts altogether.


Final Result

App shortcut demo gif

App shortcut demo

Source Code:

[su_button url=”https://github.com/SubhrajyotiSen/AppShortcuts” target=”blank” style=”flat” background=”#3973d9″ size=”6″ radius=”5″ rel=”nofollow”]View Project on GitHub[/su_button]

 

Quick Recap

In this post, we learnt

  • What App Shortcuts are
  • How are Static and Dynamic Shortcuts different
  • Implementation of both types of shortcuts
  • When to avoid using App Shortcuts and why

What do you think about App Shortcuts? Have you implemented them in your apps? I’d love to hear from you in the comments. Don’t forget to subscribe for some awesome posts coming soon.

Subhrajyoti Sen

I am an Android developer and an active Mozillian. In my free time, I contribute to Open Source and watch lots of anime.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *