How to Create Static Shortcuts in Android App? (original) (raw)

Last Updated : 15 Jul, 2025

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.

Sample-(1)-(1)

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.

Approach

**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.

android resource file

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.

Android res file

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.

AndroidManifest.xml `

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    
    <activity android:name=".Activity3"></activity>
    <activity android:name=".Activity2" />
    <activity android:name=".Activity1" />
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        
        <!-- Meta Data for linking the shortcuts.xml file to the Application Shortcuts -->
        <meta-data
            android:name="android.app.shortcuts"
            android:resource="@xml/shortcuts" />
    </activity>
</application>

`

**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.

change SDk version

**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. _ is the root element and _ 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:

Elements Description
**android:shortcutID A string literal, which represents the shortcut when a ShortcutManager object performs operations on it.
**android:enabled Determines whether the user can interact with the shortcut from a supported launcher, either _true or _false.
**android:shortcutShortLabel A concise phrase that describes the shortcut's purpose up to 10 characters.
**android:shortcutLongLabel An extended-phrase that describes the shortcut's purpose up to 25 characters.
**android:shortcutDisabledMessage 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.

vector asset

Select vector asset and choose clipart, gave it a name **icon.xml as shown below.

vector asset

**Intent Parameters:

Parameters Description
**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.

**Note:

android:targetClass="org.geeksforgeeks.static_shortcuts.MainActivity"

android:targetPackage="org.geeksforgeeks.static_shortcuts"

**Remember that input your project package name here.

shortcuts.xml `

<!-- Shortcut 1:
Navigates the user to MainActivity through the intent -->
<shortcut
    android:enabled="true"
    android:icon="@drawable/icon"
    android:shortcutId="compose0"
    android:shortcutShortLabel="@string/compose_shortcut_short_label0">
    <intent
        android:action="android.intent.action.VIEW"
        android:targetClass="org.geeksforgeeks.static_shortcuts.MainActivity"
        android:targetPackage="org.geeksforgeeks.static_shortcuts" />
</shortcut>

<!-- Shortcut 2:
Navigates the user to Activity1 through the intent -->
<shortcut
    android:enabled="true"
    android:icon="@drawable/icon"
    android:shortcutId="compose1"
    android:shortcutShortLabel="@string/compose_shortcut_short_label1">
    <intent
        android:action="android.intent.action.VIEW"
        android:targetClass="org.geeksforgeeks.static_shortcuts.Activity1"
        android:targetPackage="org.geeksforgeeks.static_shortcuts" />
</shortcut>

<!-- Shortcut 3:
Navigates the user to Activity2 through the intent -->
<shortcut
    android:enabled="true"
    android:icon="@drawable/icon"
    android:shortcutId="compose2"
    android:shortcutShortLabel="@string/compose_shortcut_short_label2">
    <intent
        android:action="android.intent.action.VIEW"
        android:targetClass="org.geeksforgeeks.static_shortcuts.Activity2"
        android:targetPackage="org.geeksforgeeks.static_shortcuts" />
</shortcut>

<!-- Shortcut 4:
Navigates the user to Activity3 through the intent -->
<shortcut
    android:enabled="true"
    android:icon="@drawable/icon"
    android:shortcutId="compose3"
    android:shortcutShortLabel="@string/compose_shortcut_short_label3">
    <intent
        android:action="android.intent.action.VIEW"
        android:targetClass="org.geeksforgeeks.static_shortcuts.Activity3"
        android:targetPackage="org.geeksforgeeks.static_shortcuts" />
</shortcut>

`

**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.k_t have the same codes.

Activity1.xml `

<!-- Activity 1-->
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="Activity 1"
    android:textSize="40sp"
    />

Activity1.kt

package org.geeksforgeeks.static_shortcuts

import androidx.appcompat.app.AppCompatActivity import android.os.Bundle

class Activity1 : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_1) } }

Activity2.xml

<!-- Activity 2-->
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="Activity 2"
    android:textSize="40sp"
    />

Activity2.kt

package org.geeksforgeeks.static_shortcuts

import androidx.appcompat.app.AppCompatActivity import android.os.Bundle

class Activity2 : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_2) } }

Activity3.xml

<!-- Activity 3-->
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="Activity 3"
    android:textSize="40sp"
    />

Activity3.kt

package org.geeksforgeeks.static_shortcuts

import androidx.appcompat.app.AppCompatActivity import android.os.Bundle

class Activity3 : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_3) } }

`

**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.

activity_main.xml `

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Main Activity"
    android:textSize="40sp"
    android:layout_centerHorizontal="true"
    android:layout_above="@id/btn2"/>

<!-- Button for navigating to Activity 1 -->
<Button
    android:id="@+id/btn1"
    android:text="Activity1"
    android:layout_centerVertical="true"
    android:layout_toLeftOf="@id/btn2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

<!-- Button for navigating to Activity 2 -->
<Button
    android:id="@+id/btn2"
    android:text="Activity2"
    android:layout_centerInParent="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

<!-- Button for navigating to Activity 3 -->
<Button
    android:id="@+id/btn3"
    android:text="Activity3"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@id/btn2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

MainActivity.kt

package org.geeksforgeeks.static_shortcuts

import android.content.Intent import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.Button

class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main)

    //Just to check if these buttons starts other activities

    // Declaring Buttons
    val btn1 = findViewById<Button>(R.id.btn1)
    val btn2 = findViewById<Button>(R.id.btn2)
    val btn3 = findViewById<Button>(R.id.btn3)

    // Intents when buttons are pressed
    
    // Takes to Activity 1
    btn1.setOnClickListener 
    {
      startActivity(Intent(this, Activity1::class.java))
    }
    
    // Takes to Activity 2
    btn2.setOnClickListener 
    {
      startActivity(Intent(this, Activity2::class.java))
    }
    
    // Takes to Activity 3
    btn3.setOnClickListener 
    {
      startActivity(Intent(this, Activity3::class.java))
    }
}

}

`

**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.

Strings.xml `

Static_Shortcuts MainActivity Activity 1 Activity 2 Activity 3

`

Output: Run on Emulator