Android App Shortcuts

If your app targets Android 7.1 (API level 25) or higher, you can define shortcuts to specific actions in your app. These shortcuts can be displayed in a supported launcher. Shortcuts let your users quickly start common or recommended tasks within your app.


Static App Shortcut

Step 1

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

<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" />

    <categories android:name="android.shortcut.conversation" />

  </shortcut>

</shortcuts>

Step 2

Add a element to this activity that references the resource file where the app’s shortcuts are defined:

<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>

 

Video Tutorial


Dynamic App Shortcut

Step 1: Shortcut


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();

Step 2: ShortcutManager

ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut));

Video Tutorial

 

Leave a Reply