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 application. Shortcuts are listed and constructed depending on the services that it provides. Shortcuts of an application (if they are built explicitly) could be seen as a list of items when long-pressed on the application icon as below.
In this article, let’s demonstrate the implementation of fixed (static) shortcuts for various activities in the application. But first, we need to know what Static here means. Static Shortcuts are a set of pre-defined shortcuts those hard-coded inside the application program. They do not change with time or user actions. They are fixed and can be accessed anytime, even if the application is not running in the background. Regarding the Shortcuts, at maximum, an application can have only 4 shortcuts. If multiple shortcuts are declared inside the program, then only the first four shortcuts are available inside the list. This is to improve the Shortcut’s visual appearance.
Step 1: Create a new project
Please refer to How to Create/Start a New Project in Android Studio for creating an Empty Activity. Note that use Kotlin as the primary language for this project.
Step 2: Create an Android Resource File (xml) in res/xml folder
Go to res -> Android Resource file and create an android resource file as shown below.
Create a file where we could store all the shortcuts. An Android Resource File which has an XML format is created where we program all the Shortcuts. The file path to save this file is res/xml/shortcuts.xml. There no need to alter any other parameter and click OK.
Once the file is generated, Shortcuts can be declared, but before, declare meta-data in the Android Manifest to link shortcuts.xml file as a resource for the application.
Step 3: Declaring shortcuts.xml file as a resource for Application Shortcuts
In the AndroidManifest.xml file, meta-data is to be declared inside the activity as shown in the following code.
Step 4: Set the Target SDK Version inside the App Gradle to 26
Shortcuts are only supported by Android Devices with API level 25 and above. For self-assurance, set the targetSDKVersion to 26.
Step 5: Configuring the shortcuts.xml file for generating different Shortcuts
Since this is an XML file, the structure of this file would be in the form of hierarchical elements. <shortcuts> is the root element and <shortcut> elements define the context of the shortcut. A shortcut mainly comprises of 6 parameters and 1 intent with 3 parameters. We now define each one of them briefly.
Parameters of a shortcut element:
A string literal, which represents the shortcut when a ShortcutManager
object performs operations on it.
Determines whether the user can interact with the shortcut
from a supported launcher, either true or false.
A concise phrase that describes the shortcut’s
purpose up to 10 characters.
An extended-phrase that describes the shortcut’s
purpose up to 25 characters.
The message that appears in a supported launcher
when the user attempts to launch a disabled shortcut.
|android:icon||Displaying icon against the shortcut.|
An icon is to be created in the res/drawable folder by right-clicking it and clicking the image/vector asset. Refer to the below image.
Select vector asset and choose clipart, gave it a name icon.xml as shown below.
|android:action||The action that the system launches when the user selects the shortcut.|
|android:targetPackage||Package of the application.|
|android:targetClass||Class of the application where the shortcut wants users to redirect.|
Code for shortcuts.xml file:
Just to visualize the shortcut, parameters shortcutID, enabled, icon, shortcutShortLabel are needed and rest can be ignored. These values are random and defined them in res/values/strings.xml file which is available in the later sections of this article. Here Intents acts the same way as they do while sending the user from one activity to another. When the user holds the application icon for 2 seconds, 4 shortcuts appear, clicking on which different activities of the application are opened. Refer to the comments in the below program.
Remember that input your project package name here.
Step 6: Creating Different Activities for Shortcuts
To check if the shortcuts are throwing the user to different activities, add 3 more Activities to the project: Activity1, Activity2, and Activity3. These activities are implemented in the above code as well. Below are the frontend codes for these activities. Activity1.kt, Activity2.kt, Activity3.kt have the same codes.
Step 7: Configuring MainActivity.kt and activity_main.xml
The MainActivity.kt is the main and the first activity of the application. Buttons are added to navigate to other activities. Refer to the comments inside the code.
Step 8: Modify the Strings.xml file
These string values(compose….label1, label2, label3, label0) declared under res/values/strings.xml define the string that is to be shown as shortcut labels for the shortcuts when the shortcut list expands.
Output: Run on Emulator
- How to Create Dynamic Shortcuts of an Android Applications?
- 10 Important Android Studio Shortcuts You Need the Most
- How to Publish Your Android App on Amazon App Store for Free?
- Android | Running your first Android app
- How to create a Facebook login using an Android App?
- How to create a Stopwatch App using Android Studio
- How to create a COVID-19 Tracker Android App
- How to create Swipe Navigation in an Android App
- How to create a Face Detection Android App using Machine Learning KIT on Firebase
- How to Create a Quiz App In Android?
- How to Create a Morse Code Converter Android App?
- How to Create a COVID-19 Tracker Android App to See Details of any City and State in India?
- How to Create a Basic Widget of an Android App?
- How to Create a Dynamic Widget of an Android App?
- How to Create a Dark Mode for a Custom Android App in Kotlin?
- Android | How to Create/Start a New Project in Android Studio?
- How to make a phone call from your Android App?
- Why Kotlin will replace Java for Android App Development
- App Development for Android in 2017: Challenges and Solutions
- Android | Creating a Calendar View app
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.