Open In App

How to Create Static Shortcuts in Android App?

Improve
Improve
Like Article
Like
Save
Share
Report

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 application with available shortcuts

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.

application gif

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




<?xml version="1.0" encoding="utf-8"?>
    package="org.geeksforgeeks.static_shortcuts">
  
    <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>
  
</manifest>


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

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




<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
  
    <!-- Shortcut 1:
    Throws the user to Main Activity 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:
    Throws the user to Activity 1 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:
    Throws the user to Activity 2 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:
    Throws the user to Activity 3 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>
      
</shortcuts>


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.

Activity1.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=".Activity1">
  
    <!-- Activity 1-->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Activity 1"
        android:textSize="40sp"
        />
  
</RelativeLayout>


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




<?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=".Activity2">
    
    <!-- Activity 2-->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Activity 2"
        android:textSize="40sp"
        />
  
</RelativeLayout>


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




<?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=".Activity3">
    
    <!-- Activity 3-->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Activity 3"
        android:textSize="40sp"
        />
  
</RelativeLayout>


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




<?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">
  
    <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"/>
  
</RelativeLayout>


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




<resources>
    <string name="app_name">Static_Shortcuts</string>
    <string name="compose_shortcut_short_label0">MainActivity</string>
    <string name="compose_shortcut_short_label1">Activity 1</string>
    <string name="compose_shortcut_short_label2">Activity 2</string>
    <string name="compose_shortcut_short_label3">Activity 3</string>
</resources>


 Output: Run on Emulator



Last Updated : 31 Aug, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads