Android App Shortcuts

Shortcuts provide direct access to app features. They help users get quick access to app functionalities.

App Shortcuts (API 25+)

App shortcuts be accessed by a long press on the app icon from the home screen or the nav drawer.

App shortcuts introduced with API 25

AndroidManifest.xml

Add a meta-data tag to the activity that contains the .action.MAIN action

<meta-data android:name="android.app.shortcuts"
android:resource="@xml/shortcuts" />

shortcuts.xml

Create this file under the res/xml folder. This file will have all your shortcuts:

<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:enabled="true"
android:icon="@drawable/shortcut_image"
android:shortcutId="shortcutId"
android:shortcutLongLabel="@string/shortcut_label_short"
android:shortcutShortLabel="@string/shortcut_label_long">
<intent
android:action="com.example.myShortcut1"
android:targetClass="com.example.myActivity"
android:targetPackage="com.example" />
<categories android:name="android.shortcut.shortcutCat" />
</shortcut>
//Add more shortcuts. Limit is up to 5 static and dynamic shortcuts.
</shortcuts>
  • Long label - Displayed on the app shortcut list.
  • Short label - Displayed when the shortcut is dragged and placed on the home screen.
  • android:action - Provides unique way to identify the app shortcut that was tapped.

These are static shortcuts but they can be created dynamically.

Activity.java

ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
Intent intent = new Intent();
intent.setAction("com.example.myShortcut1");
ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "shortcutId")
.setShortLabel("Short Label")
.setLongLabel("Open the web site")
.setIcon(Icon.createWithResource(this, R.drawable.shortcut_image))
.setIntent(intent).build();

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

Shortcut widgets (API 11+)

The official app shortcuts are not available below API 25 but they can created using widgets.

Shortcut widgets available from API 11+

AndroidManifest.xml

<activity
android:name=".activities.MyActivity"
android:icon="@drawable/shortcut_image"
android:label="@string/shortcut_label_long"
android:launchMode="singleTask"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
  • CREATE_SHORTCUT - Add this to the activity for which you intend to create a shortcut.
  • android:icon - Preview image displayed on the widget drawer.
  • android:label - Name displayed on the widget drawer

MyActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Intent intent = getIntent();
final String action = intent.getAction();
if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) {
setupShortcut();
finish();
return;
}
}
void setupShortcut() {
Intent shortcutIntent = new Intent(this, MyActivity.class);
Intent.ShortcutIconResource iconResource = Intent.ShortcutIconResource.fromContext(this, R.drawable.shortcut_image);
shortcutIntent.setAction("com.example.myShortcut1");
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent)
.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcut_label_short))
.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
setResult(RESULT_OK, intent);
}

This piece of code is executed when the user drags the shortcut widget from the widget drawer and places on the home scree.

  • EXTRA_SHORTCUT_INTENT - Intent for when the user clicks on shortcut.
  • EXTRA_SHORTCUT_NAME - Widget name.
  • EXTRA_SHORTCUT_ICON_RESOURCE - Widget image on the home screen.