Android App Shortcuts - Static, Dynamic and Pinned

Based in India.
Search for a command to run...

Based in India.
No comments yet. Be the first to comment.
I recently had to deploy a Ktor project to Railway for my hobby project. I'll share the how-to through this article. Introduction Ktor is a framework that allows us to build web applications in Kotlin, built and backed by Jetbrains. It's open-source,...

Understanding Coroutines and Flow Kotlin Coroutines and Flows are a powerful tool for managing asynchronous programming in your Android apps. This article is a beginner's guide to understanding Kotlin Flow. First, let's understand what coroutines are...

The problem When working with JVM (Java Virtual Machine), the Garbage Collector will take care of allocating and deallocating memory for us - Most of the time. In some cases, it fails to release the memory for some objects which are no longer in use,...

What are quick actions? Quick actions under the hood are App Shortcuts(in Android) and Home Screen Actions(in iOS) which allow users to perform certain app actions without having to open the app. Where have I seen this? You might have already seen an...

App shortcuts (introduced in Android Nougat 7.1) allow users to perform certain app actions without having to open the app. Developers can allow users to pin certain app actions to home screen. Let's see how.
You might have already seen and used this. Most of the popular apps have implemented this to make few of their actions more reachable to users.


There are three types of app shortcuts-
Static shortcuts - These are static shortcuts that are defined using XML and do not change. These are packed in the APK or app bundle.
Dynamic shortcuts - These shortcuts can be created/modified/removed at run-time.
Pinned shortcuts - These shortcuts can be pinned to the launcher as separate icons, if the user grants permission.
Note: Not all launchers support this feature.
In AndroidManifest.xml, under the activity whose intent filters are set to
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
add the following meta-data -
<meta-data
android:name="android.app.shortcuts"
android:resource="@xml/shortcuts" />
Now, create res/xml/shortcuts.xml
Here we'll define the shortcuts
I've created a simple shortcut with only two mandatory attributes android:shortcutId and android:shortcutShortLabel to the MainActivity for the android app.
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:shortcutId="compose"
android:shortcutShortLabel="@string/launch_main_activity">
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="me.abhishekkumar.myapplication"
android:targetClass="me.abhishekkumar.myapplication.MainActivity" />
</shortcut>
</shortcuts>
We can define multiple shortcuts like this -
<shortcuts>
<shortcut/>
<shortcut/>
<shortcut/>
</shortcuts>
android:shortcutId - It's used to uniquely identify the shortcut so that ShortcutManager object can perform operations on it.
android:shortcutShortLabel- A short label for the shortcut that will be displayed in the launcher (Recommended <= 10 characters).
Few optional attributes -
android:enabled - Using this attribute we can enable/disable a shortcut. By default, it's set to true.
android:icon- Using this attribute we can set an icon to the shortcut that will be displayed along with the label.
android:shortcutLongLabel- A long description for the shortcut (Recommended <= 25 characters). If launcher allows, it will display this of android:shortcutShortLabel
android:shortcutDisabledMessage- The message we want to display when a disabled shortcut is clicked by the user.
We can make use of the ShortcutManagerCompatJetpack library which is a helper for accessing features in ShortcutManager.
Here I've created a simple dynamic shortcut to open ShortcutActivity using ShortcutManagerCompat-
val shortcut = ShortcutInfoCompat.Builder(this, "shortcutId")
.setShortLabel("Label")
.setLongLabel("Long Label description")
.setIntent(Intent(Intent.ACTION_MAIN, Uri.EMPTY, this, ShortcutActivity::class.java))
.build()
ShortcutManagerCompat.pushDynamicShortcut(this, shortcut)
ShortcutManagerCompat.pushDynamicShortcut(context, shortcut)
Note: We need to pass the Intent action to the intent
On Android 8.0 (API level 26) and higher, we can create pinned shortcuts by following these steps -
Check if the launcher supports pinned shortcuts by using isRequestPinShortcutSupported()
Create a ShortcutInfo object in one of the two ways -
If shortcut exists - pass only the existing shortcut's ID
If shortcut doesn't exist, pass an ID, an intent, and a short label for the new shortcut.
requestPinShortcut(), we can pass a PendingIntent object, which notifies your app only when the shortcut is pinned successfully.val shortcutManager = getSystemService(ShortcutManager::class.java)
if (shortcutManager?.isRequestPinShortcutSupported==true) {
val pinShortcutInfo = ShortcutInfo.Builder(context, "shortcutId").build()
val pinnedShortcutCallbackIntent = shortcutManager.createShortcutResultIntent(pinShortcutInfo)
val successCallback = PendingIntent.getBroadcast(context,REQUEST_CODE,
pinnedShortcutCallbackIntent, REQUEST_FLAG)
shortcutManager.requestPinShortcut(pinShortcutInfo,
successCallback.intentSender)
}
val shortcutManager = getSystemService(ShortcutManager::class.java)
if (shortcutManager?.isRequestPinShortcutSupported==true) {
val pinShortcutInfo = ShortcutInfo.Builder(this, "shortcutId")
.setShortLabel("Label")
.setIntent(intent)
.build()
val pinnedShortcutCallbackIntent = shortcutManager.createShortcutResultIntent(pinShortcutInfo)
val successCallback = PendingIntent.getBroadcast(context,REQUEST_CODE,
pinnedShortcutCallbackIntent, REQUEST_FLAG)
shortcutManager.requestPinShortcut(pinShortcutInfo,
successCallback.intentSender)
}
I hope you found this article useful!
Follow me for more articles like this. Have a great day! 😊