Skip to main content

How to Create Dynamic Shortcuts in Android Apps

How to Create Dynamic Shortcuts in Android Apps

How to Create Dynamic Shortcuts in Android Apps

In today’s fast-paced mobile world, users expect quick and intuitive access to the most important parts of their favorite apps. Android’s dynamic shortcuts provide a powerful way to improve app usability and increase user engagement by offering direct access to specific app actions right from the launcher. In this detailed guide, we’ll explore how to create, update, and manage dynamic shortcuts in Android apps using Kotlin.

What Are Dynamic Shortcuts?

Introduced in Android 7.1 (API level 25), app shortcuts allow developers to add contextual, quick actions to an app's icon on the launcher. There are two kinds of app shortcuts:

  • Static Shortcuts: Defined in XML and fixed until the app is updated.
  • Dynamic Shortcuts: Created and managed programmatically at runtime, enabling flexibility based on user behavior or app state.

Dynamic shortcuts offer a personalized experience by showing shortcuts that are most relevant to the user. For instance, a messaging app could provide shortcuts to frequently contacted friends or groups.

Why Use Dynamic Shortcuts?

  • Enhanced User Experience: Enable users to jump directly to their most common tasks.
  • Increased Engagement: Encourage frequent use by reducing friction in navigation.
  • Flexible & Contextual: Change the shortcuts based on user preferences and usage patterns.

Getting Started: Setting Up Your Project

Make sure your project targets at least API level 25. Add the following to your build.gradle (Module) file:


android {
    compileSdkVersion 33

    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 33
    }
}

Since dynamic shortcuts are supported from API 25 onwards, you should include a runtime check if supporting lower API levels.

Creating Dynamic Shortcuts in Kotlin

Here is a straightforward example demonstrating how to create a dynamic shortcut that takes the user directly to a “Favorites” section of an app.


import android.content.Intent
import android.content.pm.ShortcutInfo
import android.content.pm.ShortcutManager
import android.graphics.drawable.Icon
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

    @RequiresApi(Build.VERSION_CODES.N_MR1)
    private fun createDynamicShortcut() {
        val shortcutManager = getSystemService(ShortcutManager::class.java)

        val favoriteShortcut = ShortcutInfo.Builder(this, "id_favorite")
            .setShortLabel("Favorites")
            .setLongLabel("Open Favorites Section")
            .setIcon(Icon.createWithResource(this, R.drawable.ic_favorite))
            .setIntent(
                Intent(this, FavoritesActivity::class.java).apply {
                    action = Intent.ACTION_VIEW
                }
            )
            .build()

        shortcutManager.dynamicShortcuts = listOf(favoriteShortcut)
    }
}

In this example:

  • ShortcutInfo.Builder constructs a shortcut with an ID, label, icon, and an intent to launch the Favorites screen.
  • dynamicShortcuts setter replaces any existing dynamic shortcuts with the new list.

Best Practices for Dynamic Shortcuts

  • Limit the Number: Android limits apps to a maximum of 5 dynamic shortcuts.
  • Use Meaningful IDs: Shortcut IDs must be unique and consistent to allow updates or removal.
  • Keep Icons Simple: Use clear and recognizable icons that follow Android design guidelines.
  • Update Dynamically: Change shortcuts to reflect user behavior and preferences.

Updating and Removing Shortcuts

Dynamic shortcuts can be updated or removed any time your app is running. To update, you simply create a new shortcut with the same ID but different attributes and assign it again. Here is an example that updates the shortcut with new information:


@RequiresApi(Build.VERSION_CODES.N_MR1)
fun updateFavoriteShortcut() {
    val shortcutManager = getSystemService(ShortcutManager::class.java)

    val updatedShortcut = ShortcutInfo.Builder(this, "id_favorite")
        .setShortLabel("Favorites (Updated)")
        .setLongLabel("Open Updated Favorites Section")
        .setIcon(Icon.createWithResource(this, R.drawable.ic_favorite_updated))
        .setIntent(
            Intent(this, FavoritesActivity::class.java).apply {
                action = Intent.ACTION_VIEW
            }
        )
        .build()

    shortcutManager.updateShortcuts(listOf(updatedShortcut))
}

To remove shortcuts all at once or selectively:


@RequiresApi(Build.VERSION_CODES.N_MR1)
fun removeShortcut() {
    val shortcutManager = getSystemService(ShortcutManager::class.java)
    
    // To remove a specific shortcut by ID
    shortcutManager.removeDynamicShortcuts(listOf("id_favorite"))

    // To remove all dynamic shortcuts
    shortcutManager.removeAllDynamicShortcuts()
}

Creating Pinned Shortcuts: Going One Step Further

Besides dynamic shortcuts that show up on the launcher automatically, Android allows users to pin shortcuts explicitly to the launcher for quicker access.

Here’s how to request a pinned shortcut:


@RequiresApi(Build.VERSION_CODES.O)
fun requestPinShortcut() {
    val shortcutManager = getSystemService(ShortcutManager::class.java)

    if (shortcutManager.isRequestPinShortcutSupported) {
        val pinnedShortcut = ShortcutInfo.Builder(this, "id_pinned")
            .setShortLabel("Pinned Shortcut")
            .setIcon(Icon.createWithResource(this, R.drawable.ic_pin))
            .setIntent(
                Intent(this, PinnedActivity::class.java).apply {
                    action = Intent.ACTION_VIEW
                }
            )
            .build()

        shortcutManager.requestPinShortcut(pinnedShortcut, null)
    }
}

This allows users to create shortcuts on their home screen manually, giving them full control over personalization.

Handling API Compatibility

Since dynamic shortcuts require API 25 or above, it’s essential to guard your code with appropriate checks:


if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
    createDynamicShortcut()
}

For devices running lower API levels, you might consider alternative navigation aids within your app, such as in-app widgets or navigation drawers.

Conclusion

Dynamic shortcuts are a powerful yet underrated feature in Android development that helps enhance app engagement and usability. They offer a personalized, quick access mechanism that can drastically reduce the user’s interaction steps. By carefully designing your shortcuts to reflect user habits and app workflows, you provide a smarter, more productive mobile experience.

With straightforward Kotlin APIs, adding and managing dynamic shortcuts doesn’t require complex work, but the benefits in user experience can be significant. Start incorporating dynamic shortcuts into your Android app today to delight your users with smarter navigation and boost overall app productivity.

Happy coding!

Comments

Popular posts from this blog

How to Download Apk file from Url and Install Programmatically

In this post we learn about download apk file from server or website and then install it Programmatically in Phone. Sometimes we have to download external apk file from server and then install if downloading successfully finished.For this we use AsyncTask class  for background process. So here is Code Snippet for this task.Lets Start :- Before this we have to add these Permissions in Manifest.xml file : <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> DownloadNewVersion.java class DownloadNewVersion extends AsyncTask<String,Integer,Boolean> { @Override protected void onPreExecute() { super.onPreExecute(); bar = new ProgressDialog(getActivity()); bar.setCancelable(false); bar.setMessage("Downl...

Working with Android 11 (Changes and Security Features)

 Hello everyone , I am here with new article which is hot topic nowadays "Android 11" .The stable version of Android.  Android 11 is the eleventh major release and 18th version of Android, the mobile operating system developed by the Open Handset Alliance led by Google. It was released on September 8, 2020.It is comes with many security features and other features as well . And it is now compulsory in play store  to upload new apps with API lavel 30 which is compatible with Android 11 and from November onwards old apps also have to update with API 30 .Some other guidelines you can check out from here . Play Store Guidelines So its clear that we have to update our apps with API level 30 .But Android 11 comes with some changes as well which we have to do in our projects. For example from Android developer site "Android 11 (API level 30) further enhances the platform, giving better protection to app and user data on external storage. ". Scoped storage enforcement: Apps...

How to Disable Edge-to-Edge Mode in Android 15 and Android 16

How to Disable Edge-to-Edge Mode in Android 15 and Android 16 How to Disable Edge-to-Edge Mode in Android 15 and Android 16 With each new Android version, Google continues to enhance UI experiences by embracing modern, immersive design principles. Starting with Android 10, edge-to-edge mode has become the standard, allowing apps to extend their content beneath system bars for a more immersive display. In Android 15 and the upcoming Android 16 releases, edge-to-edge mode remains a core part of the UI framework, leveraging gestures and providing richer visual experiences. However, as developers, there are times when we may want to disable edge-to-edge mode — whether for legacy app compatibility, ensuring content fidelity, or managing complex UI layouts. In this detailed guide, we'll explore how you can disable edge-to-edge mode in Android 15 and Android 16. We will cover the background of edge-to-edge, why you might want to di...