App Shortcuts in Android
Android 7.1 (API 25) introduced us to a new feature called App shortcuts. This feature is very useful and simplifies user’s experience. Now, get ready to use it in your applications!
Here, we will explore what is an App shortcut and how you can use it with a simple example.
What is an App Shortcut?
An App shortcut in Android Nougat is similar to 3D touch in iOS and allows display of any app’s most common actions or tasks on the user’s home screen.
Users can add/modify/delete shortcuts by a long press on the app icon. It allows you to drag any app shortcut to the home screen and create a new shortcut icon that is the new entry point for that app.
It allows you to publish five shortcuts which can be a combination of ‘Static’ and ‘Dynamic’ shortcuts.
Static App Shortcuts
They are defined in the resource file of an app’s APK and remain constant throughout the application.
Static shortcuts are immutable which means that you must wait until you update your entire app to change the details of these static shortcuts.
Why use Static App shortcut
Static shortcuts are used for generic actions of the app that remains persistent until you update your app.
Dynamic App Shortcuts
Dynamic shortcuts are created at run-time of your app. You can publish, update, and remove these shortcuts as you use these applications. It uses ShortcutManager API.
Why use Dynamic App shortcut
Dynamic shortcuts are used to provide specific actions within your app that could be changed based on user’s interaction within the app.
Implementing App Shortcuts
Static Shortcuts
To create a static shortcut,
- add a meta-data tag to your launcher Activity in the manifest and provide the shortcuts resource file.
<meta-data
android:name="android.app.shortcuts"
android:resource="@xml/shortcuts" />
- Create a new resource file: res/xml/shortcuts.xml.
- 1. This is where we will specify all shortcuts that your app can support.
- 2. Each <shortcut> element contains information about a static shortcut, including its icon, its description labels, and the intents that it launches within the app.
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:icon="@drawable/add_event"
android:shortcutId="shortcut_add_events"
android:shortcutLongLabel="@string/longLabel_view_events"
android:shortcutShortLabel="@string/shortLabel_view_events">
<intent
android:action="android.intent.action.VIEW"
android:targetClass="com.priyanka.appshortcutdemo.AddEvents"
android:targetPackage="com.priyanka.appshortcutdemo" />
</shortcut></shortcuts>
These are very easy steps to implement. However, you may notice that as you press the Back button on your phone, you will be taken to the Home Screen.
Now, if you want to navigate it within your app then we can add multiple intent tags under the shortcut once we previously created.
<shortcut
android:icon="@drawable/add_event"
android:shortcutId="shortcut_add_events"
android:shortcutLongLabel="@string/longLabel_view_events"
android:shortcutShortLabel="@string/shortLabel_view_events">
<intent
android:action="android.intent.action.MAIN"
android:targetPackage="com.priyanka.appshortcutdemo"
android:targetClass="com.priyanka.appshortcutdemo.MainActivity"/>
<intent
android:action="android.intent.action.VIEW"
android:targetClass="com.priyanka.appshortcutdemo.AddEvents"
android:targetPackage="com.priyanka.appshortcutdemo" />
</shortcut>
Once you have followed above steps, your phone’s screen will look like an image shown below:
Dynamic Shortcuts
To create dynamic shortcut, you need to use ShortcutManager API. You will need to define each shortcut using ‘Shortcutinfo’ that provides UI information of the shortcut to your phone.
You can also use multiple intent tags to navigate within the application. Follow these steps to create a Dynamic App Shortcut using MainActivity.onCreate() method.
Change your shortcut preference
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "shortcut_view_events")
.setShortLabel("View Events")
.setLongLabel("View Events")
.setRank(1)
.setIcon(Icon.createWithResource(this,R.drawable.view_event))
.setIntents(new Intent[]{new Intent(Intent.ACTION_VIEW, Uri.EMPTY,this,MainActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK),
new Intent(Intent.ACTION_VIEW, Uri.EMPTY, this, ViewEvents.class)
})
.build();
So, creating app shortcut was this easy! Now, run it with your phone on Android Nougat.