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.
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.
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”)
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.
Output: Run on Emulator
- How to Create Static Shortcuts in Android App?
- 10 Important Android Studio Shortcuts You Need the Most
- How to install Android Applications in Mobile Phone without USB Cables using Android Studio
- How to Create a Dynamic Widget of an Android App?
- Android | How to Create/Start a New Project in Android Studio?
- Dynamic CheckBox in Android with Examples
- Android | Android Application File Structure
- Android | AdMob Interstitial Ads for Android Studio
- Android | Running your first Android app
- Android | How to add Radio Buttons in an Android Application?
- Android | How to Request permissions in Android Application?
- Difference between Android 1.1 and Android 4.0.1
- Difference between Android 1.1 and Android 2.3.4
- Difference between Android 1.0 and Android 1.1
- Difference between Android 1.0 and Android 7.1
- Difference between Android 1.0 and Android 2.3
- Difference between Android 1.0 and Android 2.2
- Difference between Android 1.0 and Android 1.6
- Difference between Android 1.0 and Android 2.1
- Difference between Android 1.0 and Android 2.0.1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.