Android 7.1 Static Shortcut

My shiny Pixel has arrived, heres a quick example on how to add a static shortcut to an app.

“Never taken a shortcut before?”

Static shortcuts are defined in xml. So whack your xml in /res/xml/shortcuts.xml

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:shortcutId="quickStart"
android:enabled="true"
android:icon="@drawable/ic_vector_shortcut"
android:shortcutShortLabel="@string/quickstart"
android:shortcutLongLabel="@string/quickstart_long"
android:shortcutDisabledMessage="@string/quickstart_disabled">
<intent
android:action="com.your.app.QUICKSTART"
android:targetPackage="com.your.app"
android:targetClass="com.your.app.activities.MainActivity" />
</shortcut>
<!-- Specify more shortcuts here. -->
</shortcuts>

Its all pretty straight forward, in the example I’m using a custom action so that I can check inside the activity for the intent (“com.your.app.QUICKSTART”).

Next up we need to add a reference to this new xml file in the AndroidManifest. Add this just after your intent-filter in your main activity, before the closing </activity>

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

Now to handle the shortcut. In the activity you’re starting add a new constant for the custom intent action.

private static final String ACTION_QUICKSTART = "com.your.app.QUICKSTART";

Inside the onCreate method, check the intent for the custom intent action and do your thing…

if (ACTION_QUICKSTART.equals(getIntent().getAction())){
startQuickStart();
}

Hey presto, a simple app shortcut

Flutter Fan Boy & Android Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store