Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java for Android
  • Android Studio
  • Android Kotlin
  • Kotlin
  • Flutter
  • Dart
  • Android Project
  • Android Interview
Open In App
Next Article:
How to Create Dynamic Shortcuts of an Android Applications?
Next article icon

How to Create Dynamic Shortcuts of an Android Applications?

Last Updated : 22 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

New tab and Incognito tab are shortcuts available for Chrome

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:

  1. setShortLabel: The string that appears against the shortcut when the application is held.
  2. setIcon: The image that appears against the shortcut when the application is held.
  3. setIntent: The Activity to which the shortcut redirects.
  4. 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


Next Article
How to Create Dynamic Shortcuts of an Android Applications?

A

aashaypawar
Improve
Article Tags :
  • Kotlin
  • Android
  • Android Projects

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 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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences