Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to Create Dynamic Shortcuts of an Android Applications?

  • Last Updated : 08 Sep, 2020

In Android Phones, when an Application is held for more than a second, certain app actions appear in a list. These app actions are nothing but shortcuts for performing and actions without opening the application. Shortcuts for an application are a list of features (quick services) that helps the users to easily and quickly jump to particular features or activities within the application. Shortcuts are listed and constructed depending on the services that it provides. Refer to the screenshot below.

New tab and Incognito tab are shortcuts available for Chrome

These shortcuts can be pre-defined or hardcoded and never changes (Static) in its entire life cycle and hence are termed as Static Shortcuts. Another set of shortcuts, that might change with time or context are called Dynamic Shortcuts. In this article, we shall discuss and implement Dynamic Shortcuts in an Android Application. The primary language for implementing the application shall be Kotlin.

Note: To create static shortcuts of an Android App please refer to How to Create Static Shortcuts in Android App?

Concept Behind the Implemented Application

In this project create an Android App where we have only one activity i.e. the MainActivity displaying 2 Buttons, Click and Append. We clicked neither of them. What is programmed inside the application is to show 2 shortcuts initially, and We first check if these 2 shortcuts are shown. We then open the application and click the “Click” button and close the app. We again check for the shortcuts and now, they are changed to and The Click button was programmed to change the shortcuts dynamically. We again open the app and click the Append button and close the app. A new shortcut, NewlyAppended (test case) is appended to the list of Instagram and AskFM. In this way number of shortcuts as well as the context within them can be changed. Typically, Android System accepts multiple shortcuts (programmed inside the app), but for the UX, it shows only 4 of them.

dynamic shortcut


Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.

Step 2: Working with activity_main.xml file

As we have discussed before add 2 Buttons, one “Click” and another “Append” in the activity_main.xml file. The complete activity_main.xml file is given below.


<?xml version="1.0" encoding="utf-8"?>
    <!--Defining 2 Buttons, Click and Append-->
        android:text="click" />
        android:text="append" />

Step 3: Working with MainActivity.kt file

  • To build a shortcut, two elements ShortcutManager and ShortcutBuilder are required. Refer to the below structure (programmed in Kotlin):

var shortcutManager = getSystemService(

      var sampleShortcut = ShortcutInfo.Builder(applicationContext,”sampleID”)



              .setIntent(Intent(Intent.ACTION_VIEW, ….sampleIntent…..))


shortcutManager!!.dynamicShortcuts = listOf(sampleShortcut)

There are four basic elements within a Shortcut:

  1. setShortLabel: The string that appears against the shortcut when the application is held.
  2. setIcon: The image that appears against the shortcut when the application is held.
  3. setIntent: The Activity to which the shortcut redirects.
  4. build: Build a Shortcut with given entities.
  • We have created 4 Web Intents that redirect to 4 different social websites Facebook, Instagram, AskFM, and one test case “NewlyAppended” for appending another intent into the current list.
  • In the “MainActivity.kt” file, declare them and set on-click Listeners so that actions can take place when the buttons are clicked.
  • Shortcuts are directly declared into the “MainActivity.kt” file.
  • Now that all the modules are ready, we can define our shortcuts in the “MainActivity.kt” file
  • The listof is a list of declared shortcut to be shown in that list. The number of shortcuts to be declared has no limitations, it can be 1 or even 1000, but for UX, the system allows only up to 4 shortcuts to be shown. Now refer to the codes below, comments are added for understanding every element. Below is the complete code for the MainActivity.kt file.


package org.geeksforgeeks.dynamic_shortcuts
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.widget.Button
import androidx.annotation.RequiresApi
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        // Shortcut Manager for managing the shortcuts
        var shortcutManager = getSystemService(
        // Defining a shortcut, Shortcut 1
        var shortcut1 = ShortcutInfo.Builder(applicationContext, "ID1")
                .setIcon(Icon.createWithResource(applicationContext, R.drawable.icon))
                .setIntent(Intent(Intent.ACTION_VIEW, Uri.parse("")))
        // Defining a shortcut, Shortcut 2
        var shortcut2 = ShortcutInfo.Builder(applicationContext, "ID2")
                .setIcon(Icon.createWithResource(applicationContext, R.drawable.icon))
                .setIntent(Intent(Intent.ACTION_VIEW, Uri.parse("")))
        // Show list of shortcuts when held
        shortcutManager!!.dynamicShortcuts = listOf(shortcut1, shortcut2)
        // When btn is clicked, changes are made to Shortcut 1 and Shortcut 2
        val btn = findViewById<Button>(
        btn.setOnClickListener {
            shortcut1 = ShortcutInfo.Builder(applicationContext, "ID1")
                    .setIcon(Icon.createWithResource(applicationContext, R.drawable.icon))
                    .setIntent(Intent(Intent.ACTION_VIEW, Uri.parse("")))
            shortcut2 = ShortcutInfo.Builder(applicationContext, "ID2")
                    .setIcon(Icon.createWithResource(applicationContext, R.drawable.icon))
                    .setIntent(Intent(Intent.ACTION_VIEW, Uri.parse("")))
            shortcutManager!!.dynamicShortcuts = listOf(shortcut1, shortcut2)
        // When add is clicked, a new shortcut is appended to the existing list of shortcuts
        val add = findViewById<Button>(
        add.setOnClickListener {
            var shortcut3 = ShortcutInfo.Builder(applicationContext, "ID3")
                    .setIcon(Icon.createWithResource(applicationContext, R.drawable.icon))
                    .setIntent(Intent(Intent.ACTION_VIEW, Uri.parse("")))
            shortcutManager!!.dynamicShortcuts = listOf(shortcut1, shortcut2, shortcut3)

Output: Run on Emulator

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!