How to Create Dynamic Shortcuts of an Android Applications?
Last Updated : 22 Jul, 2024
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.

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, Ask.fm and Instagram.com. 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 Facebook.com and Google.com. 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.
Approach
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.
activity_main.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <!--Defining 2 Buttons, Click and Append--> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="click" /> <Button android:id="@+id/append" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/btn" android:layout_centerHorizontal="true" android:text="append" /> </RelativeLayout>
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(ShortcutManager::class.java)
var sampleShortcut = ShortcutInfo.Builder(applicationContext,”sampleID”)
.setShortLabel(“sampleName”)
.setIcon(Icon.createWithResource(applicationContext,R.drawable.sampleIcon))
.setIntent(Intent(Intent.ACTION_VIEW, ….sampleIntent…..))
.build()
shortcutManager!!.dynamicShortcuts = listOf(sampleShortcut)
There are four basic elements within a Shortcut:
- setShortLabel: The string that appears against the shortcut when the application is held.
- setIcon: The image that appears against the shortcut when the application is held.
- setIntent: The Activity to which the shortcut redirects.
- 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.
MainActivity.kt package org.geeksforgeeks.dynamic_shortcuts import android.content.Intent import android.content.pm.ShortcutInfo import android.content.pm.ShortcutManager import android.graphics.drawable.Icon import android.net.Uri import android.os.Build import android.os.Bundle import android.widget.Button import androidx.annotation.RequiresApi import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { @RequiresApi(Build.VERSION_CODES.N_MR1) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Shortcut Manager for managing the shortcuts var shortcutManager = getSystemService(ShortcutManager::class.java) if (shortcutManager != null) { // Defining a shortcut, Shortcut 1 var shortcut1 = ShortcutInfo.Builder(applicationContext, "ID1") .setShortLabel("Instagram") .setIcon(Icon.createWithResource(applicationContext, R.drawable.icon)) .setIntent(Intent(Intent.ACTION_VIEW, Uri.parse("https://www.instagram.com"))) .build() // Defining a shortcut, Shortcut 2 var shortcut2 = ShortcutInfo.Builder(applicationContext, "ID2") .setShortLabel("AskFM") .setIcon(Icon.createWithResource(applicationContext, R.drawable.icon)) .setIntent(Intent(Intent.ACTION_VIEW, Uri.parse("https://www.ask.fm"))) .build() // 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>(R.id.btn) btn.setOnClickListener { shortcut1 = ShortcutInfo.Builder(applicationContext, "ID1") .setShortLabel("Google") .setIcon(Icon.createWithResource(applicationContext, R.drawable.icon)) .setIntent(Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.com"))) .build() shortcut2 = ShortcutInfo.Builder(applicationContext, "ID2") .setShortLabel("Facebook") .setIcon(Icon.createWithResource(applicationContext, R.drawable.icon)) .setIntent(Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com"))) .build() shortcutManager!!.dynamicShortcuts = listOf(shortcut1, shortcut2) } // When add is clicked, a new shortcut is appended to the existing list of shortcuts val add = findViewById<Button>(R.id.append) add.setOnClickListener { var shortcut3 = ShortcutInfo.Builder(applicationContext, "ID3") .setShortLabel("NewlyAppended") .setIcon(Icon.createWithResource(applicationContext, R.drawable.icon)) .setIntent(Intent(Intent.ACTION_VIEW, Uri.parse("https://www.newlyAppended.com"))) .build() shortcutManager.dynamicShortcuts = shortcutManager.dynamicShortcuts + shortcut3 } } }
Output: Run on Emulator
Similar Reads
How to Create a Dynamic Widget of an Android App?
Widgets are the UI elements provided by an application for accessing some of its features remotely either from Home Screens or Lock Screens. Widgets can be Static or Dynamic meaning that the display elements don't change (Static) or change (Dynamic) with time. Through this article, let's demonstrate
3 min read
How to Create Static Shortcuts in Android App?
An application might contain several services for the user and to facilitate a user quickly to those services, shortcuts are used. 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 appl
7 min read
How to Create Google Lens Application in Android?
We have seen the new Google Lens application in which we can capture images of any product and from that image, we can get to see the search results of that product which we will display inside our application. What are we going to build in this article? We will be building a simple application in w
10 min read
How to Make a Phone Call From an Android Application?
In this article, let's build a basic android application that allows users to make phone calls directly from the app. This is accomplished with the help of Intent with action as ACTION_CALL. Basically Intent is a simple message object that is used to communicate between android components such as ac
5 min read
How to Send an Email From an Android Application?
In this article, we will make a basic Android Application that can be used to send email through your android application. You can do so with the help of Implicit Intent with action as ACTION_SEND with extra fields: email id to which you want to send mail,the subject of the email, andbody of the ema
4 min read
How to Create Multiple APK Files For Android Application?
In Android, making a single apk compatible with all devices, increases the apk size, as it has resources for every device. Why make our users download apk that contain device-specific useless resources who are having lower memory devices. Now we got the solution i.e. creating multiple apks from the
3 min read
How to Share a Captured Image to Another Application in Android?
Pre-requisite: How to open a Camera through Intent and capture an image In this article, we will try to send the captured image (from this article) to other apps using Android Studio. Approach: The image captured gets stored on the external storage. Hence we need to request permission to access the
6 min read
How to Add Capabilities to Shortcuts in Android 13?
Shortcuts have been an integral part of Android Apps, almost every app we use has shortcuts that facilitate main activity opening with various time-savers, which turn out to be great time savers. Over the years this experience has greatly improved, and now we can add even more capabilities to our ap
4 min read
How to Change Application's Starting Activity in Android?
Many times while building an android application. We have to change the default activity of our application to another activity that we have created within our android studio project. So that we can set that custom activity to the starting activity of our application. In this article, we will take a
2 min read
How to Create a Basic Widget of an Android App?
Widgets are the micro-version of the application that consists of some functionality of the application that is displayed only on the Home Screens or the Lock Screen. For example, we see Weather, Time, and Google Search Bars on the Home Screen, and FaceLock, and FingerprintLock on the Lock Screen, w
5 min read