Creating App Shortcuts in Launcher


Android 7.1 allows you to define shortcuts to specific actions in your app. These shortcuts can be displayed in a supported launcher, such as the one provided with Nexus and Pixel devices. Shortcuts let your users quickly start common or recommended tasks within your app:

To implement it you have to start with downloading the latest SDK (API level 25). And after updating the SDK you have to update your build.gradle file to point the latest version.

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"

    defaultConfig {
        applicationId ""
        targetSdkVersion 25


Static shortcut

In your app’s manifest file (AndroidManifest.xml), find an activity whose intent filters are set to the android.intent.action.MAIN action and the android.intent.category.LAUNCHERcategory

        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    <meta-data android:name=""
        android:resource="@xml/shortcut" />

Your shortcut.xml will be under xml under res folder and will look like this

<shortcuts xmlns:android=""
        <categories android:name="android.shortcut.conversation" />
Specify shortcut categories. Currently only SHORTCUT_CATEGORY_CONVERSATION is defined in the framework. In the above Intent if your Activity is expecting some extras you can always pass it with extra tag like this.
             android:value="value" />

Static Shortcuts

So this will create a static shortcut. So we also have Dynamic Shortcuts as well lets see what are they. If you want to always show a shortcut in your app you can go with static shortcut, if you want some shortcuts to be Dynamic like text to a person which is Dynamic not same for all then you have to use Dynamic Shortcuts.

Dynamic Shortcut

To create a Dynamic Shortcut you will have to use these codes in your activity where you like to initialize the Shortcut.

private void createShorcut() {
    ShortcutManager sM = getSystemService(ShortcutManager.class);

    Intent intent1 = new Intent(getApplicationContext(), Activity1.class);

    ShortcutInfo shortcut1 = new ShortcutInfo.Builder(this, "shortcut1")
            .setLongLabel("Shortcut 1")
            .setShortLabel("This is the shortcut 1")
            .setDisabledMessage("Login to open this")
            .setIcon(Icon.createWithResource(this, R.drawable.shortcut1))


ShortcutManager uses system service so this has to be in an activity and remember this only works on Android 7.1 so it's good to add the annotation @TargetApi(25) to this code to avoid compile errors and add version check before calling these methods.

if (Build.VERSION.SDK_INT >= 25) {

Also remember the Intent action setAction(); has to be set. Remember users can pin the shortcuts as well, so it is dynamic or static they can always pin it, even if you remove dynamic shortcuts users will still have it if pinned. So if you are using Dynamic Shortcuts and you can’t do certain actions you have to disable those shortcuts. And that is where the Disabled messages get displayed.

private void removeShorcuts() {
    ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
So the method disableShortcuts(List<String>) will disable the shortcuts whose id is passed. removeAllDynamicShortcuts(); will remove all the dynamic shortcuts. See the Images below for example, left is normal shortcuts for a logged in user and right has two disabled shortcuts for Guest users and when pressed it displays the disabled messages set in the method setDisabledMessage(String).

All shortcuts enabled Some shortcuts disabled

So this is how we use the new feature app shortcuts on Android 7.1, an app can have 5 shortcuts. So you can use setRank(int) on ShortcutInfo to change the order of the shortcuts.


Check out the following links for more details:

Fork me on GitHub