With the new Android version, Nougat 7.1 (API 25), Google gives us some interesting possibilities. One of them is the option to create shortcuts to an application. In this post I will show you how static shortcuts work.
Shortcuts in Android – what do you need to know?
Android shortcuts allow you to automatically transfer user to desired location within your application. Among others, this allows to grant user faster access to frequently used functionalities of the application, saving the user from performing steps necessary to get to a particular activity.
Android shortcuts can be divided into two types: static and dynamic. They are completely different in terms of how we build them. Static shortcuts are created via XML, as opposed to dynamic ones, which can be created in the application code at any point. In today’s post I will focus on static shortcuts.
Creating static shortcuts
To add static shortcuts to an application, as I have already mentioned, you need to create a right XML. This file must be created under the path /res/xml/shortcuts.xml. All static shortcuts are defined there. Below you can see two examples of shortcuts.
<?xml version="1.0" encoding="utf-8"?> <shortcuts xmlns:android="http://schemas.android.com/apk/res/android"> <shortcut android:shortcutId="enableShortcut" android:enabled="true" android:icon="@android:drawable/star_on" android:shortcutShortLabel="@string/short_shortcut_label" android:shortcutLongLabel="@string/long_shortcut_label" android:shortcutDisabledMessage="@string/shortcut_disabled"> <intent android:action="android.intent.action.MAIN" android:targetPackage="com.example.dominika.shortcuts" android:targetClass="com.example.dominika.shortcuts.MainActivity" /> <intent android:action="android.intent.action.VIEW" android:targetPackage="com.example.dominika.shortcuts" android:targetClass="com.example.dominika.shortcuts.ShortcutActivity" /> </shortcut> <shortcut android:shortcutId="disableShortcut" android:enabled="false" android:icon="@android:drawable/star_off" android:shortcutShortLabel="@string/disable_shortcut_label" android:shortcutDisabledMessage="@string/shortcut_disabled"> <intent android:action="android.intent.action.VIEW" android:targetPackage="com.example.dominika.shortcuts" android:targetClass="com.example.dominika.shortcuts.ShortcutActivity" /> </shortcut> </shortcuts>
At this stage you should pay attention to several basic parameters:
- flag enabled – defines whether a shortcut is available,
- icon – defines the icon that appears to the left of the name in the list of shortcuts in the applications, or simply the main icon after isolating our shortcut on the screen,
- shortcutShortLabel – mandatory, the short name of our shortcut,
- shortcutLongLabel – optional field, which is visible only if the name fits on the screen,
- shortcutDisabledMessage – a message that will appear if a user tries to run unavailable shortcut,
- intent – key elements determining what the shortcut leads to.
Next you should take into account the file created in the manifest. When you open the AndroidManifest.xml, you need to add the following code, still within the main activity:
<meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcuts"/>
In this example, created are two shortcuts that lead to created example ShortcutActivity. The second one is set as not available and leads directly to the new activity. The first one, in turn, is given 2 intents, that will ensure that you create a back stack of the application. It reflects usual start of two activities in a defined order (firstly MainActivity, then ShortcutActivity).
Below you can see the obtained result:
You may notice that if a shortcut is not available, it does not appear in the list of shortcuts. However, each shortcut can be isolated on the screen, then (with shortcut unavailable) the android icon is grayed out, and after clicking on it a user sees TOAST with defined message.
Android Nougat provided us with a few novelties. Shortcuts to applications are certainly one of the more interesting ideas. As you can see adding a static shortcut is not complicated. I encourage you to try to create your own shortcuts.