Android Native - How to add static App shortcuts

dimitrilc 2 Tallied Votes 187 Views Share

Introduction

Starting on API level 25, static Shortcuts can be used to quickly navigate to a specific point in your app. In this tutorial, we will learn how to create static shortcuts for an Android app.

Goals

At the end of the tutorial, you would have learned:

  1. How to create static shortcuts.

Tools Required

  1. Android Studio. The version used in this tutorial is Arctic Fox 2020.3.1 Patch 3.

Prerequisite Knowledge

  1. Basic Android.

Project Setup

To follow along with the tutorial, perform the steps below:

  1. Create a new Android project with the default Empty Activity.

Static Shortcuts

Static shortcuts are simply icons that allow your users to quickly go to a specific point in your app. For example, in the animation below, Search, Subscriptions, and Explore are all shortcuts.

shortcut.gif

In supported launchers, shortcuts are shown when the app icon is held down by the end user.

Static shortcuts should only be used to navigate to content that stays the same during the lifetime of your app. There are also two other types of shortcuts, dynamic shortcuts and pinned shortcuts, but they are out of scope for this tutorial.

Creating a static shortcut

The steps to create a static shortcut, at the minimum, include:

  1. Create an XML file to house the shortcut(s) declarations.
  2. Identifying the Intent for your shortcut.
  3. Bind shortcuts to their intents.
  4. Provide the meta-data key-value pair to the application entry point activity in the manifest file.

shortcuts.xml

To create the shortcuts resource for the static shortcuts, follow the steps below.

  1. In the Android view, right-click on res > New > Android Resource Directory.

  2. Both the Directory name and the Resource type should have the value xml.

  3. Because static shortcuts were only added in API level 25, this new Resource Directory must contain the qualifier v25. Scroll all the way to the bottom of the Available qualifiers list, add the API 25 qualifier. You will see the Directory name changes automatically to xml-v25.
    image1.png

  4. Select OK.

  5. Right click on the xml resource directory > New > XML Resource File.

  6. Set both the File name and the root element to shortcuts.
    image3.png

  7. Select OK.

  8. Open the file shortcuts.xml (v25).

  9. Inside the shortcuts element is where we need to add our individual <shortcut> tags. Copy and paste the code below inside <shortcuts>.

     <shortcut></shortcut>
  10. The <shortcut> element is currently empty. For a static shortcut, we must add at least the two required attributes, android:shortcutId and android:shortcutShortLabel.

  11. Add these two attributes to <shortcut>. Please note that android:shortcutId cannot reference a String resource, while android:shortcutShortLabel must reference a String resource. Your code will not compile if these rules are not followed.

     android:shortcutId="Shortcut ID"
     android:shortcutShortLabel="@string/shortcut_label">
  12. Open up the file strings.xml and add the String resource used by android:shortcutShortLabel previously.

     <string name="shortcut_label">Example Shortcut Label</string>

The shortcut Intent

The <shortcut> element supports an inner element called the <intent> element. This element requires an android:action attribute at the minimum.

To keep the code simple, we can just have the action open up the Main activity of our app. To do that, add the <intent> below as an inner element of <shortcut>.

<intent
   android:action="android.intent.action.VIEW"
   android:targetPackage="com.example.daniwebstaticshortcut"
   android:targetClass="com.example.daniwebstaticshortcut.MainActivity" />

Add meta-data to the manifest

The very last step in our project would be to add the required shortcuts information to the App entry point in the manifest file. To do that, follow the steps below:

  1. Open up the file app/manifests/AndroidManifest.xml

  2. Find the <activity> with the <intent-filter> below.

     <action android:name="android.intent.action.MAIN" />
     <category android:name="android.intent.category.LAUNCHER" />
  3. Add the <meta-data> element below as an inner element of <activity>.

     <meta-data android:name="android.app.shortcuts"
        android:resource="@xml/shortcuts" />

Test the shortcut

We have completed all of the steps required to create a static shortcut. It is time to see if it works.

  1. Launch the app.
  2. Press Home to return to the home screen.
  3. If the app icon is not on the launcher screen, add it to the launcher screen.
  4. Hold down your cursor/finger on the icon until the static shortcut appears.

static_shortcut.gif

If your static shortcut is not showing, check the Solution Code section to verify that you are not missing anything.

Solution Code

shortcuts.xml (v25)

<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
   <shortcut
       android:shortcutId="Shortcut ID"
       android:shortcutShortLabel="@string/shortcut_label">
       <intent
           android:action="android.intent.action.VIEW"
           android:targetPackage="com.example.daniwebstaticshortcut"
           android:targetClass="com.example.daniwebstaticshortcut.MainActivity" />
   </shortcut>
</shortcuts>

strings.xml

<resources>
   <string name="app_name">Daniweb Static Shortcut</string>
   <string name="shortcut_label">Example Shortcut Label</string>
</resources>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.daniwebstaticshortcut">

   <application
       android:allowBackup="true"
       android:icon="@mipmap/ic_launcher"
       android:label="@string/app_name"
       android:roundIcon="@mipmap/ic_launcher_round"
       android:supportsRtl="true"
       android:theme="@style/Theme.DaniwebStaticShortcut">
       <activity
           android:name=".MainActivity"
           android:exported="true">
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />

               <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
           <meta-data android:name="android.app.shortcuts"
               android:resource="@xml/shortcuts" />

       </activity>
   </application>

</manifest>

Summary

We have learned how to create static shortcuts. The project only uses required minimum attributes for shortcut elements. You can play around and add other things such as icons if you wish.

The full project code can be found here https://github.com/dmitrilc/DaniwebStaticShortcut

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.