Adding Shortcut to App Icon

Adding shortcuts to app icon in the launcher feature is added in the Android Nougat 7.1.1.

It is good feature in android. By using this feature we can provide user to access directly to any screen in our app. We can add max 5 shortcuts for the app which includes both static and dynamic shortcuts.

App shortcuts on Nexus 6P

Image: Source from the developer.android.com

Reference:Shortcuts

Shortcuts are 2 types.

Static shortcuts :

Statics shortcuts we define the in xml/<name of file>.xml and this xml file should be refer from the manifest.xml file in launcher activity. Static shortcuts are can’t be removed dynamically. If we want to remove we have to update the app with removing this shortcuts.

Example:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.myapplication">
  <application ... >
    <activity android:name="Main">
      <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>
  </application>
</manifest>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
  <shortcut
    android:shortcutId="compose"
    android:enabled="true"
    android:icon="@drawable/compose_icon"
    android:shortcutShortLabel="@string/compose_shortcut_short_label1"
    android:shortcutLongLabel="@string/compose_shortcut_long_label1"
    android:shortcutDisabledMessage="@string/compose_disabled_message1">
    <intent
      android:action="android.intent.action.VIEW"
      android:targetPackage="com.example.myapplication"
      android:targetClass="com.example.myapplication.ComposeActivity" />
    <!-- If your shortcut is associated with multiple intents, include them
         here. The last intent in the list determines what the user sees when
         they launch this shortcut. -->
    <categories android:name="android.shortcut.conversation" />
  </shortcut>
  <!-- Specify more shortcuts here. -->
</shortcuts>

Dynamic shortcuts:

Dynamic shortcuts we can add and remove dynamically. Which is more useful when we want to predict more user operations and give those in the shortcut. Like for contacts app we can find which contact user calling more then we can add that contact in the shortcut.

Dynamic shortcuts we can add and remove dynamically.

Dynamic shortcuts we can create using ShortcutManager API. Which provides set of methods for add, set, remove and other features.

Example:

ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);

ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "id1")
    .setShortLabel("Web site")
    .setLongLabel("Open the web site")
    .setIcon(Icon.createWithResource(context, R.drawable.icon_website))
    .setIntent(new Intent(Intent.ACTION_VIEW,
                   Uri.parse("https://www.mysite.example.com/")))
    .build();

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