How to add a Static Shortcut in Android 7.1


Here is a quick example on how to add a static shortcut to an app. In Android nougat 7.1 new api to create app shortcut but it is static means you can not change after.

source

App Shortcuts is a means of exposing common actions or tasks of your application to the launcher. Your users can reveal the shortcuts by long-pressing the app's launcher icon.
They are of two types:
static: defined statically in a resource file; cannot be changed unless you modify the file and redeploy the app

dynamic: published at runtime; shortcuts can be updated without the need of redeploying the app


Moving forward  we will create a xml file because static shortcut only work on resource xml. 
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.androidlad.QUICKSTART" android:targetPackage="com.your.app" android:targetClass="com.androidlad.activities.MainActivity" /> </shortcut> <!-- Specify more shortcuts here. --> </shortcuts>


You can see that the root tag of this file is <shortcuts>, which can hold multiple <shortcut> blocks. Each of them, as you may have guessed, represents a static shortcut. Here, the following properties can be set on one shortcut:
enabled: as the name states, whether the shortcut is enabled or not. If you decide to disable your static shortcut you could either set this to false, or simply remove it from the <shortcuts> set

icon: the icon shown on the left hand side of the shortcut. In my case, I created a simple Vector Drawable from within Android Studio and assigned it as an icon

shortcutDisabledMessage: when you disable your shortcut, it will disappear from the ones that the user can reveal by long pressing your application icon, but one can pin a shortcut to the launcher (by long pressing and dragging it on the desired launcher page), so when disabled the pinned shortcut will appear greyed out and upon tapping it a Toast with this message will appear

shortcutLongLabel: longer text of the shortcut shown when the launcher has enough space

shortcutShortLabel: a concise description of the shortcut. The field is mandatory. Most probably this will be the one which will appear on your launcher

intent: here you define your intent (or more intents) that your shortcut will open upon being tapped

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>


<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.catinean.appshortcutsdemo"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> <meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcuts" /> </activity> </application> </manifest>
Final screen after executing above code.






 Isn't it awesome ?




Please let me know your review on this.


Previous
Next Post »

3 comments

Click here for comments
Unknown
admin
7 February 2017 at 08:38 ×

I have TabbedActivity and I want to set tab to shortcut. How to make this?

Reply
avatar
Unknown
admin
7 February 2017 at 08:41 ×

And maybe You know how to set a method to shortcut?

Reply
avatar
Unknown
admin
4 March 2017 at 20:46 ×

Hi Wuiko,
You need to add your TabbedActivity Activity in Shortcut xml.








Cheers

I would happy to help you in future keep in touch. You can also reach me to futorapps@gmail.com

Reply
avatar