Android: App Shortcuts

June 7, 2018

App Shortcuts Title Image

Starting from Android 7.1 Google added App Shortcuts to Android. When you use a launcher that supports them, you can longpress an app icon and then you will see a floating widget with useful shortcuts.

Static shortcuts

You have the option to choose between static and dynamic shortcuts. When you already know, that only those few shortcuts will be available and there will be no change, you are good to go with the static ones.

To begin with you have to add a reference to your app’s AndroidManifest.xml to the activity that has the Launcher category:

<activity
    android:name=".MainActivity"
    android:theme="@style/AppTheme.BaseTheme">
    <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>

And then you just have to create your shortcuts.xml file

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:targetApi="25">
    <shortcut
        android:enabled="true"
        android:icon="@drawable/icon_action"
        android:shortcutId="shortcut_name"
        android:shortcutShortLabel="@string/short_cut_action">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass=".MainActivity"
            android:targetPackage="com.example.myapp">
        </intent>
    </shortcut>
    <!-- Add more shortcuts here -->
</shortcuts>

Don’t forget that you can add just up to 5 shortcuts, more will crash the app.

Dynamic shortcuts

This option might be more interesting for the most of the developers out ther, because it allows you to add user related shortcuts and actions. A special shortcut for pro useres or a quick access shortcut to your favorite book? No problem!

private void makeShortcuts() {
  if (Build.VERSION.SDK_INT >= 25) {
    ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);

    Intent intent1 = new Intent(getApplicationContext(), ShortcutHandler.class); //specify a intent to use with the shortcut
    intent1.setAction(Intent.ACTION_VIEW);

    ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "shortcuts_id")
        .setShortLabel("Name")
        .setLongLabel("This is the shortcuts name")
        .setIcon(Icon.createWithResource(context, R.drawable.icon_shortcut))
        .setIntent(intent1)
        .build();

    //you can ofcourse create more than 1 shortcut, but keep in mind that it is not possible to create more than 5
    //just add them also to the List that you pass to setDynamicShortcuts

    shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut));
  }
}

Now that you know how to create new shortcuts you can also

.addDynamicShortcuts(List<ShortcutInfo>)

.updateShortcuts(List<ShortcutInfo>) They need to have the same id

.removeDynamicShortcuts(List<String>) Remove all shortcuts with specified id

.removeAllDynamicShortcuts() Remove all shortcuts

Disable/Enable shortcuts

Sometimes a user might not be allowed to use a shortcut, for example when he logged out or a trial expired:

.disableShortcuts(List<String>) or you can also define a disabled message:

.disableShortcuts(List<String>, “Sorry, you have to log in!”)

To (re-)enable the shortcuts just call .enableShortcuts(List<String>)