Print Friendly, PDF & Email

Here in this blog, we are discussing one of the features of Android Nougat 7.1 that is App Shortcuts. The App shortcuts will allow the users to access primary actions straight from their launcher by long pressing the app icon.

It’s very helpful for the users to access those actions very quickly and complete their tasks at outside of the app without entering into it. Users can also pin these shortcuts to the home screen even for quick navigation.

In this tutorial, I will describe you to create both static and dynamic App shortcuts, for that just follow the things which I am going to explain here. So, are you ready to ride with us? let’s start and keep accelerate your enthusiasm until the end of this tutorial.

Types of Shortcuts

We have 3 types of Shortcuts in the API.

1. Static Shortcuts: These shortcuts are predefined generated from the XML file that you create. These shortcuts are only changeable until updating the new version of the app because they come together with your .apk.

2. Dynamic Shortcuts: The name implies that they are changeable at any time(runtime). They can be created or destroyed at runtime, no need to wait until the new update of the app.

3. Pinned Shortcuts:  These are generated completely at runtime, the user can create itself by copying the static or dynamic shortcuts onto the launcher.

we are allowed only to publish 5 shortcuts (combination of static and dynamic shortcuts) at a time for the app and there is no limitation for Pinned shortcuts.

Note: App must have minimum SDK version set to API 25.

Create New Android Project

Follow the sequence of steps to create an AndroidProject.

1. First, open Android Studio on your pc.

2. Go to File menu->New->New Project

3. Application name: AppShortcutsDemo.

4. Company domain: Your package name.

5. Then click on Next button.

6. Minimum SDK: Android 7.1.1 (Nougat).

7. Then again click on the Next button.

8. Choose the Activity type as Empty Activity, afterward click on Next button.

9. Activity Name: MainActivity.

10. Finally, click on Finish button.

Now, this will create your Android application Project named as AppShortcutsDemo.

Build.gradle

The app level build.gradle file of my app shows here.

apply plugin: 'com.android.application'
android {
    compileSdkVersion 25
    buildToolsVersion '25.0.0'

    defaultConfig {
        applicationId "com.appshortcutsdemo"
        minSdkVersion 25
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:25.1.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
}

Static Shortcuts

1. To create Static Shortcuts first we need to make changes in Android Manifest file.

2. Add a <meta-data> tag in your MainActivity which is the launcher activity in my case name attribute as android.app.shortcuts

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.loginworks.appshortcutsdemo" >
    <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/AppTheme" >
        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
                <meta-data
                    android:name="android.app.shortcuts"
                    android:resource="@xml/shortcuts"/>
            </intent-filter>
        </activity>
    </application>
</manifest>

3. Now we need to create the resource file where res/xml/shortcuts.xml the shortcuts are defined.

4. After creation is done, open the file and copy the below code.

shortcuts.xml

We are creating two Static Shortcuts here one is for Contact Us and other is About.

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:targetApi="25">
    <shortcut
        android:shortcutId="contact"
        android:enabled="true"
        android:icon="@mipmap/ic_launcher"
        android:shortcutLongLabel="@string/contact_short"
        android:shortcutShortLabel="@string/contact_long"
        android:shortcutDisabledMessage="@string/shortcut_contact_disabled_msg">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass=".ContactUs"
            android:targetPackage="com.loginworks.appshortcutsdemo">
        </intent>
        <categories android:name="android.shortcut.conversation" />
    </shortcut>
    <shortcut
        android:shortcutId="about"
        android:enabled="true"
        android:icon="@mipmap/ic_launcher"
        android:shortcutLongLabel="@string/about_short"
        android:shortcutShortLabel="@string/about_long"
        android:shortcutDisabledMessage="@string/shortcut_about_disabled_msg">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass=".AboutUs"
            android:targetPackage="com.loginworks.appshortcutsdemo">
        </intent>
        <categories android:name="android.shortcut.conversation" />
    </shortcut>
</shortcuts>

5. Let’s understand the attributes used in the shortcut tag one by one.

a) shortcutId: This attribute serves as the ID for the shortcut and it should be unique.

b) enabled: It describes to enable or disable the shortcut. If enabled=”false” the shortcut will not show in the App Shortcuts list when the user long presses the app icon.

c) icon: This attribute is used to set the icon for the App Shortcut.

d) shortcutShortLabel: It is used for displaying the name of the shortcut but there is one limitation with this, It allows only less than or equal to 10 characters.

e) shortcutLongLabel: To overcome the above-mentioned problem we use this attribute but this also allows less than or equal to 25 characters only.

f) shortcutDisabledMessage: The message, which is shown to the user when a disabled pinned shortcut was selected

g) intent: The intent that will be invoked when user will click the shortcut icon.

6. Each <shortcut> tag at least needs one <intent> tag specifying which activity has to be open when the user clicks on the shortcut. Without at least one <intent> the app will crashes.

Dynamic Shortcuts

1. As I said earlier that dynamic shortcuts can be created or deleted at any time while running the application.

2. We will need to use the ShortcutManager API to perform add, update and delete at runtime.

3. setDynamicShortcuts() is used to redefine the dynamic shortcuts.

4. updateShortcuts() exactly it performs the update of dynamic shortcuts.

5. removeDynamicShortcuts() It removes the existed shortcuts. 

MainActivity.java

1. Here we are creating two dynamic shortcuts one is opening for Google site and other is used to open SettingsActivity screen

2. Open the MainActivity file and paste the below code in onCreate() method.

3. ShortcutInfo.Builder() is used to define the properties of the App Shortcut

package com.loginworks.appshortcutsdemo;
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.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.Arrays;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
        ShortcutInfo shortcut = new ShortcutInfo.Builder(this,"id1")
                .setShortLabel("Google")
                .setLongLabel("Open Google")
               .setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))
                .setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.com")))
                .build();
        ShortcutInfo settingsShortcut = new ShortcutInfo.Builder(this,"id2")
                .setShortLabel("Settings")
                .setLongLabel("open Settings")
                .setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))
                .setIntents( new Intent[]{
                        new Intent(Intent.ACTION_MAIN, Uri.EMPTY, this, SettingsActivity.class)
                })
                .build();
        shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut, settingsShortcut));
    }
}

Purpose

The user always loves to use the App Shortcuts because it reduces the number of taps to the user as well as time.

Limitations

1. Only allows us to create 5 App Shortcuts and it is highly recommended to use 4 App shortcuts.

2. Another limitation is the launcher, all the launchers do not support the App Shortcuts yet.

Testing

1. It is the last process that we need to do here to check the results of our work.

2. To test the App Shortcuts please use the device which is running on Android 7.1 version, with the supported launcher.

3. So, let’s run or build the app by clicking the Run button or use shift+F10 in Android studio.

4. After running the app first, it will show the app icon as shown above.

5. when we long press on the app icon then it will open the App Shortcuts what we created in the app.

6. In these four, two are dynamic and two are static App Shortcuts.

7. Moreover, if we click on the App shortcuts it will take you to the respective Activity what we described at intent.

8. For suppose if we click on the settings shortcut then it will open the respective activity as shown above.

9. Here what we are seeing in the above image is Pinned Shortcut which is used for quick navigation to the Settings activity.

Conclusion

I hope that you are well enough to work with App Shortcuts in Android after completing this. If you have any queries, please feel free to drop your comments below. Gracias for keeping your enthusiasm to reach the end of this blog.

  •  
  •  
  •  
  •  
  •  

LEAVE A REPLY

Please enter your comment!
Please enter your name here