Code Example

Kotlin Android ShortcutManager Snippets

Learn ShortcutManager usage via these simple snippets.

How to open a Shortcut:

    fun openShortcut(view: View) {
        val intent = Intent(this, ShortcutActivity::class.java)
        startActivity(intent)
    }

Here is how you instantiate a ShortcutManager:

  val manager = getSystemService(Context.SHORTCUT_SERVICE) as ShortcutManager

Here is how you build Shortcut info:

            val info = ShortcutInfo.Builder(this, "shortcut-id")
                    .setShortLabel("label")
                    .setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher_round))
                    .setIntent(intent)
                    .build()

Here is how you create a Shortcut using the requestPinShortcut():

    fun createShortcut(view: View) {
        // ShortcutManager#requestPinShortcut needs API 26
        if (Build.VERSION.SDK_INT < 26) {
            return
        }

        val manager = getSystemService(Context.SHORTCUT_SERVICE) as ShortcutManager
        if (manager.isRequestPinShortcutSupported) {
            val intent = Intent(this, ShortcutActivity::class.java).apply {
                action = Intent.ACTION_VIEW
            }

            val info = ShortcutInfo.Builder(this, "shortcut-id")
                    .setShortLabel("label")
                    .setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher_round))
                    .setIntent(intent)
                    .build()
            manager.requestPinShortcut(info, null)
        }
    }

Here is how you create a Shortcut the old way:

    fun createShortcutOld(view: View) {
        val targetIntent = Intent(this, ShortcutActivity::class.java).apply {
            action = Intent.ACTION_VIEW
        }
        val icon = Intent.ShortcutIconResource.fromContext(this, R.mipmap.ic_launcher_round)
        val intent = Intent("com.android.launcher.action.INSTALL_SHORTCUT").apply {
            putExtra(Intent.EXTRA_SHORTCUT_INTENT, targetIntent)
            putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon)
            putExtra(Intent.EXTRA_SHORTCUT_NAME, "label")
        }
        sendBroadcast(intent)
    }

1. Full Example

Here is the demo screenshot:

This example will comprise the following files:

  • MainActivity.kt
  • ShortcutActivity.kt

Step 1: Create Project

  1. Open your AndroidStudio IDE.
  2. Go to File-->New-->Project to create a new project.

Step 2: Add Dependencies

No special dependency is needed.

Step 3: Design Layouts

*(a). activity_main.xml

Create a file named activity_main.xml and design it as follows:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.github.spitson.takeo.myshortcut.MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="MainActivity"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="129dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="127dp"
        android:layout_marginStart="128dp"
        android:onClick="openShortcut"
        android:text="Open another"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:layout_editor_absoluteX="128dp"
        tools:layout_editor_absoluteY="296dp"
        android:layout_marginTop="30dp"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="createShortcut"
        android:text="Create Shortcut"
        tools:layout_editor_absoluteX="116dp"
        tools:layout_editor_absoluteY="370dp"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginStart="116dp"
        android:layout_marginEnd="115dp"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginTop="27dp"
        app:layout_constraintTop_toBottomOf="@+id/button" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="100dp"
        android:layout_marginTop="28dp"
        android:onClick="createShortcutOld"
        android:text="Create Old Shortcut"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button2"
        tools:layout_editor_absoluteX="102dp"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginStart="102dp" />

</android.support.constraint.ConstraintLayout>

*(b). activity_shortcut.xml

Create a file named activity_shortcut.xml and design it as follows:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.github.spitson.takeo.myshortcut.ShortcutActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Shortcut Activity"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

Step 4: Write Code

Write Code as follows:

*(a). MainActivity.kt

Create a file named MainActivity.kt

Here is the full code

package com.github.spitson.takeo.myshortcut

import android.content.Context
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.content.Intent
import android.content.pm.ShortcutInfo
import android.view.View
import android.content.pm.ShortcutManager
import android.graphics.drawable.Icon
import android.os.Build

class MainActivity : AppCompatActivity() {

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

    fun openShortcut(view: View) {
        val intent = Intent(this, ShortcutActivity::class.java)
        startActivity(intent)
    }

    fun createShortcut(view: View) {
        // ShortcutManager#requestPinShortcut needs API 26
        if (Build.VERSION.SDK_INT < 26) {
            return
        }

        val manager = getSystemService(Context.SHORTCUT_SERVICE) as ShortcutManager
        if (manager.isRequestPinShortcutSupported) {
            val intent = Intent(this, ShortcutActivity::class.java).apply {
                action = Intent.ACTION_VIEW
            }

            val info = ShortcutInfo.Builder(this, "shortcut-id")
                    .setShortLabel("label")
                    .setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher_round))
                    .setIntent(intent)
                    .build()
            manager.requestPinShortcut(info, null)
        }
    }

    fun createShortcutOld(view: View) {
        val targetIntent = Intent(this, ShortcutActivity::class.java).apply {
            action = Intent.ACTION_VIEW
        }
        val icon = Intent.ShortcutIconResource.fromContext(this, R.mipmap.ic_launcher_round)
        val intent = Intent("com.android.launcher.action.INSTALL_SHORTCUT").apply {
            putExtra(Intent.EXTRA_SHORTCUT_INTENT, targetIntent)
            putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon)
            putExtra(Intent.EXTRA_SHORTCUT_NAME, "label")
        }
        sendBroadcast(intent)
    }
}

*(b). ShortcutActivity.kt

Create a file named ShortcutActivity.kt

Here is the full code

package com.github.spitson.takeo.myshortcut

import android.os.Bundle
import android.support.v7.app.AppCompatActivity

class ShortcutActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_shortcut)
    }
}

Run

Simply copy the source code into your Android Project,Build and Run.

Reference

Download code here.
Follow code author here.