On 20th October 2016, my Nexus 5x got Android 7.1 and I was so excited to see what is inside and downloaded the update. And then I saw some major tweaks on the Phone dialer and then the App Shortcuts.

Yes, from Android 7.1 app can have shortcuts, Shortcuts lets you quickly start common or recommended tasks within an app. I saw some google apps has it already and then checked the developers blog and how to integrate it, it was straight so easy and straightforward, so thought I will give a try on it.

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 "com.example.app"

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.LAUNCHER category.

<activity

android:name=".SplashActivity"

android:screenOrientation="portrait"

android:theme="@style/AppTheme.BrandedLaunch">

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

</activity>

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

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">

<shortcut

android:enabled="true"

android:icon="@drawable/icon_name"

android:shortcutId="shortcut_name"

android:shortcutShortLabel="@string/name">

<intent

android:action="android.intent.action.VIEW"

android:targetClass=".MainActivity"

android:targetPackage="com.example.app">

</intent>

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

</shortcut>

</shortcuts>

In the above Intent if your Activity is expecting some extras you can always pass it with extra tag like this.

<intent

...

<extra

android:name="key"

android:value="value" />

</intent>

So this will create a shortcut and this is called static shortcut according to the documentation. So we have something called 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.

@TargetApi(25)

private void createShorcut() {

ShortcutManager sM = getSystemService(ShortcutManager.class);



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

intent1.setAction(Intent.ACTION_VIEW);



ShortcutInfo shortcut1 = new ShortcutInfo.Builder(this, "shortcut1")

.setIntent(intent1)

.setShortLabel(getString(R.string.shortcut1))

.setLongLabel("Shortcut 1")

.setShortLabel("This is the shortcut 1")

.setDisabledMessage("Login to open this")

.setIcon(Icon.createWithResource(this, R.drawable.shortcut1))

.build();



sM.setDynamicShortcuts(Arrays.asList(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) {

createShorcut();

}

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

removeShorcuts();

}

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.

@TargetApi(25)

private void removeShorcuts() {

ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);

shortcutManager.disableShortcuts(Arrays.asList("shortcut1"));

shortcutManager.removeAllDynamicShortcuts();

}

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.