Open In App

How to Create Dynamic Shortcuts of an Android Applications?

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.

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.




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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

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.




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)
  
        // 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 = listOf(shortcut1, shortcut2, shortcut3)
        }
    }
}

Output: Run on Emulator


Article Tags :