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


    Senior Android Software Engineer (f/m/d)

    Paradox Cat GmbH
    Munich
    • Full Time
    apply now

    Mobile Engineer

    OLX Group
    Remote, Portugal, Spain, Romania, Poland
    • Full Time
    apply now

    Android Software Engineer (f/m/d)

    Paradox Cat GmbH
    Munich
    • Full Time
    apply now

OUR VIDEO RECOMMENDATION

, ,

From Scoped Storage to Photo Picker: Everything to know about Storage

Persistence is a core element of every mobile app. Android provides different APIs to access or expose files with different tradeoffs.
Watch Video

From Scoped Storage to Photo Picker: Everything to know about Storage

Yacine Rezgui
Android developer advocate
Google

From Scoped Storage to Photo Picker: Everything to know about Storage

Yacine Rezgui
Android developer ad ...
Google

From Scoped Storage to Photo Picker: Everything to know about Storage

Yacine Rezgui
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. Required fields are marked *

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

Menu