Blog Infos
Author
Published
Topics
Published
Quick actions into your application

We’ve seen shortcuts that appear on WhatsApp, Facebook, Google Chrome, etc on our applications. They are useful when you want quick navigations into the application without going through the long process once instead. Implementing is not difficult at all; we just need to add some configurations that will do the trick.

Static Shortcuts

These are shortcuts that don’t change based on the data of the application. Like opening a new tab on Google Chrome. They will always be present for you on the home screen.

  1. Let’s start by creating an xml-v25 folder under res/; and create a file called shortcuts.xml. You can name this file anything you wish, but the folder names must be exact.
  2. Instead of <resources></resources>, we need to use the <shortcuts></shortcuts> tag instead. All our static shortcuts will be defined here.
  3. Let’s define a basic shortcut by adding a <shortcut/> tag here.
<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:shortcutId="mainShortcut"
android:enabled="true"
android:shortcutShortLabel="@string/shortcut_main_short_label">
</shortcut>
</shortcuts>
view raw shortcuts1.xml hosted with ❤ by GitHub

Each shortcut will need a shortcutId and a shortcutShortLabel.

4. Inside this, we need to define an IntentIntents are ways you can enter the application with the shortcut. For the basic one, let’s simply add an intent to the MainActivity with an extra.

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:shortcutId="mainShortcut"
android:enabled="true"
android:shortcutShortLabel="@string/shortcut_main_short_label">
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="com.nishant.shortcuts"
android:targetClass="com.nishant.shortcuts.MainActivity">
<extra android:name="content_key" android:value="Msg from shortcut" />
</intent>
</shortcut>
</shortcuts>
view raw shortcuts2.xml hosted with ❤ by GitHub

When the user clicks on the shortcut that says “Main”, then an Intent will be sent to the MainActivity with the key content_key and the String extra “Msg from shortcut”

5. Retrieving the extra in MainActivity. We can simply get the extra as we normally do:

val shortcutExtra = intent.getStringExtra("content_key") ?: ""
textView.text = shortcutExtra

6. Don’t forget to register this in your AndroidManifest.xml. You must add the shortcuts configuration within the launcher activity declaration.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nishant.shortcuts">
<application ...>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- Adding shortcuts config inside launcher activity -->
<meta-data
android:name="android.app.shortcuts"
android:resource="@xml/shortcuts" />
</activity>
</application>
</manifest>
view raw shortcut3.xml hosted with ❤ by GitHub

Job Offers

Job Offers


    Mobile Developer Android (m/w/d) Firebase

    REWE digital
    Köln / Cologne
    • Full Time
    apply now

    Distinguished Android Engineer

    Expedia Group
    Chicago, London, San Francisco, Austin, Gurgaon, Seattle or Remote
    • Full Time
    apply now

    Senior Compiler Engineer C++/LLVM – Leuven

    Guardsquare
    Leuven, Belgium
    • Full Time
    apply now
Load more listings

OUR VIDEO RECOMMENDATION

, ,

The Evolution of Android Graphics in Android 12/13

Android 12 and 13 both added significant new capabilities to Android platform graphics, including RenderEffect, RuntimeShader, and more. At the same time, RenderScript has been deprecated and we’ve introduced the RenderScript Intrinsics Replacement Toolkit. This…
Watch Video

The Evolution of Android Graphics in Android 12/13

Daniel Galpin
Android Developer Advocate and Fast Talking YouTuber
Google

The Evolution of Android Graphics in Android 12/13

Daniel Galpin
Android Developer Ad ...
Google

The Evolution of Android Graphics in Android 12/13

Daniel Galpin
Android Developer Advocat ...
Google

Jobs

Voilà! You have now added a static shortcut to your application

Dynamic Shortcuts

Alongside Static Shortcuts, you may also want to create some Shortcuts whose data is dependent on a network call, for example. In WhatsApp, you have 3 shortcuts to the most recent chats — which are always changing. In this scenario, you would want to specify the shortcut via code.

const val SHORTCUT_SECOND_ID = "id_second"
@RequiresApi(Build.VERSION_CODES.N_MR1)
fun createNewShortcut() {
val shortcutManager = getSystemService(context, ShortcutManager::class.java)
val secondActivity = ShortcutInfo.Builder(context, SHORTCUT_SECOND_ID)
.setShortLabel("Main")
.setIntents(arrayOf(
/* you should start with the Launcher Activity */
Intent(Intent.ACTION_VIEW, null, context, MainActivity::class.java),
Intent(Intent.ACTION_VIEW, null, context, SecondActivity::class.java)
))
.build()
shortcutManager!!.dynamicShortcuts = listOf(secondActivity)
}
view raw shortcut4.kt hosted with ❤ by GitHub

We are applying the same logic as we did before with the Static shortcuts. However, here you can pass parameters and set a custom label and custom intents based on the action you would want to have.

I hope you enjoyed reading my article. Thank you! ✌️

Want to connect?
My GitHub profile.
My Portfolio website.

This article was originally published on proandroiddev.com on June 03, 2022

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
Shortcuts in the app are designed to do common tasks in the app from…
READ MORE

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

Menu